python编程

全局禁止警告

import warnings
warnings.simplefilter(action='ignore', category=FutureWarning)

读csv文件

with open('address', 'r') as csvfile:
    reader = csv.reader(csvfile)

读xlsx读到dataframe

import pandas as pd

#path = '/Users/luliang/Documents/工作/烟台港/2期/data/倒罐指令.xlsx'

excelFile = r'/Users/luliang/Documents/工作/烟台港/2期/data/倒罐指令.xlsx'
df = pd.DataFrame(pd.read_excel(excelFile))
print(df)

打印输出到文件

import sys


class Logger(object):
    def __init__(self, filename='default.log', stream=sys.stdout):
        self.terminal = stream
        self.log = open(filename, 'w')

   def write(self, message):
        self.terminal.write(message)
        self.log.write(message)

   def flush(self):
        pass


sys.stdout = Logger('10-18推动开班库存.log', sys.stdout)
sys.stderr = Logger('10-18推动开班库存.log_file', sys.stderr)

pandas省略号问题

pd.set_option('display.max_rows', None)  # 可以填数字,填None表示'行'无限制
pd.set_option('display.max_columns', None) # 可以填数字,填None表示'列'无限制

pd.set_option('display.width', 1000) #横向不换行

按照列属性输出

all10 = pd.read_csv('/Users/luliang/Desktop/工作/数据清洗/推动事件20191211-3/anji_推动开班库存20191220.csv',
                    usecols=['零件代码', '零件类型', '是否翻包装零件', '点检区', '溢库区', '主库位', '主库位类型', 'LOC班组', '有无二级缓存点', '隔离区', '料箱面积',
                             '托盘宽度', '托盘最大装箱数', 'CMC空箱区'])
print(all10[(all10.isnull().values == True)])
print('----------------------------------')

有判断值输出

all11 = pd.read_csv('/Users/luliang/Desktop/工作/数据清洗/推动事件20191211-3/anji_推动开班库存20191220.csv',
                    usecols=['零件类型', '暂存区', '主库位编组位置'])
print(all11[(all11.零件类型 != '发动机') & ((all11.暂存区.isnull().values == True) | (all11.主库位编组位置.isnull().values == True))])
print('----------------------------------')

2维转置

#创建数据
df = pd.DataFrame([['A', 1, 2], ['B', 3, 4]],  columns=['Name', 'c1', 'c2'])
#操作
df2 = pd.DataFrame(df.values.T, index=df.columns, columns=df.index)

Dataframe 多行合并为一行

#根据一列来合并
def ab(df):
        return','.join(df.values)
    df = df.groupby(['uid'])['sub'].apply(ab)
    df = df.reset_index()

pandas多个矩阵根据一列合并join

#根据一列来合并
resf = res.join(ldadd1.set_index('mapuid'),on='mapuid',how='left')

pandas多个行转成一行

.set_index(['tide','time','mapuid','type'])['num'].unstack().reset_index()

postgreSQL

import psycopg2
pg_host = "xxx"
pg_port = "xxx"
pg_user = "xxx"
pg_password = "xxx"
pg_database = "xxx"
pg_table = ""


def gpcon(host, port, user, password, database,sql):
    
    conn_string = "host=" + host + " port=" + port + " dbname=" + database + " user=" + user + " password=" + password
    gpconn = psycopg2.connect(conn_string)

    curs = gpconn.cursor()

    curs.execute(sql)

    data = curs.fetchall()

    gpconn.commit()

    curs.close()
    gpconn.close()
    data = pd.DataFrame(data)
    return data

mysql

import mysql.connector
 
mydb = mysql.connector.connect(
  host="localhost",
  user="root",
  passwd="123456",
  database="runoob_db"
)
mycursor = mydb.cursor()
 
mycursor.execute("SELECT * FROM sites")
 
myresult = mycursor.fetchall()     # fetchall() 获取所有记录
 
for x in myresult:
  print(x)

空值补0

A7_fin['banggong'].fillna(0,inplace = True)

str转date

datetime.datetime.strptime(str,'%Y-%m-%d')

时间加减

datetime.datetime.strptime(time_start,'%Y-%m-%d')+datetime.timedelta(days=3)

date转str

datetime.datetime.strftime(time_start,'%Y-%m-%d')

pandas使用to_csv输出文件中文乱码问题

df.to_csv("test.csv",encoding="utf_8_sig")

将pandas数据框中的多个列更改为datetime

data['birth_date'] = pd.to_datetime(data[birth_date], errors ='coerce')

Python学习系列(1)判断输入是否全为数字、字符等

s为字符串

s.isalnum()  所有字符都是数字或者字母,为真返回 True,否则返回 False。

s.isalpha()   所有字符都是字母,为真返回 True,否则返回 False。

s.isdigit()     所有字符都是数字,为真返回 True,否则返回 False。

s.islower()    所有字符都是小写,为真返回 True,否则返回 False。

s.isupper()   所有字符都是大写,为真返回 True,否则返回 False。

s.istitle()      所有单词都是首字母大写,为真返回True,否则返回 False。

s.isspace()   所有字符都是空白字符,为真返回 True,否则返回 False。

md5

result1 = []
for n in data['did'].tolist():
    if n.isdigit() == True and len(n)==15:
        print(n)
        #h1
        h1.update(n.encode('utf-8'))
        result1.append(h1.hexdigest())

横向合并concat

df1_ = pd.DataFrame([['a', 1], ['b', 2]],columns=['letter', 'number'])
df1_

在这里插入图片描述

df2_ = pd.DataFrame([['c', 3], ['d', 4]],columns=['let', 'num'])
df2_

在这里插入图片描述

df3_ = pd.concat([df1_,df2_],axis=1)
df3_

在这里插入图片描述

汉字转拼音

# 不带声调的(style=pypinyin.NORMAL)
def pinyin(word):
    s = ''
    for i in pypinyin.pinyin(word, style=pypinyin.NORMAL):
        s += ''.join(i)
    return s


# 带声调的(默认)
def yinjie(word):
    s = ''
    # heteronym=True开启多音字
    for i in pypinyin.pinyin(word, heteronym=True):
        s = s + ''.join(i) + " "
    return s

GP库连接

def gpcon(host, port, user, password, database, sql):
    conn_string = "host=" + host + " port=" + port + " dbname=" + database + " user=" + user + " password=" + password
    gpconn = psycopg2.connect(conn_string)
    curs = gpconn.cursor()
    curs.execute(sql)

    gpdata = curs.fetchall()

    gpconn.commit()

    curs.close()
    gpconn.close()

    return gpdata

MYSQL 下载数据decode问题

 class MyConverter(mysql.connector.conversion.MySQLConverter):

    def row_to_python(self, row, fields):
        row = super(MyConverter, self).row_to_python(row, fields)

        def to_unicode(col):
            if type(col) == bytearray:
                return col.decode('utf-8')
            return col

        return [to_unicode(col) for col in row]

MYSQL 库连接

 def mysqlcon(host, port, user, password, database, sql, ret):
    mysqlcon = mysql.connector.connect(
        host=host,
        port=port,
        user=user,
        passwd=password,
        database=database, use_unicode=False, converter_class=MyConverter
    )
    mysqlcurs = mysqlcon.cursor(buffered=True)
    # mysql

    mysqlcurs.execute(sql)

    if ret is True:

        myresult = mysqlcurs.fetchall()  # fetchall() 获取所有记录
        return myresult
        mysqlcon.close()
        mysqlcurs.close()

    elif ret is False:

        mysqlcon.commit()
        mysqlcon.close()
        mysqlcurs.close()

从列表里移除单词

 def remove_word(alllist):
    regex = '角色ID'
    for num in range(len(alllist)):
        if re.search(regex, alllist[num]) is not None:
            alllist[num] = 'used'
    data_dic = []
    regex = ':'
    for num in range(len(alllist)):
        if re.search(regex, alllist[num]) is not None:
            # print(alllist[num])
            data_dic.append(alllist[num])
    fin_dic = []
    for num in range(len(data_dic)):
        if re.search('[0-9]', data_dic[num]) is not None:
            # print(data_dic[num])
            fin_dic.append(data_dic[num])
    return fin_dic

pandas寻找位置

 def find_pd(word, pd):
    index_ = []
    for num_row in range(pd.shape[0]):  # 5
        for num_col in range(pd.shape[1]):  # 4
            if pd.iloc[num_row, num_col] == word:
                print([num_row, num_col])
                index_.append([num_row, num_col])
            else:
                pass
    return index_

RUN

if __name__ == '__main__':

markdown 在字母正下方插入下标

$\underset {\theta}{min}$

抽奖

import random
from collections import Counter

N = 1000000

def rand_num():
    n_l,e_l  = [],[]
    while len(n_l)<5:
        t = random.randint(1,35)
        if t in n_l:
            pass
        else:
            n_l.append(t)
    n_l.sort()
    while len(e_l)<2:
        t = random.randint(1,12)
        if t in e_l:
            pass
        else:
            e_l.append(t)
    e_l.sort()
    l = n_l +e_l
    return l

sum_ = []
for n in range(N):
    sum_.append(str(rand_num()))
    
dict_ = {}
for key in sum_:
    dict_[key] = dict_.get(key, 0) + 1
 
def top_n_scores(n, score_dict):
    lot = [(k,v) for k, v in dict_.items()] #make list of tuple from scores dict
    nl = []
    while len(lot)> 0:
        nl.append(max(lot, key=lambda x: x[1]))
        lot.remove(nl[-1])
    return nl[0:n]   

top_n_scores(4, dict_)  

多行公式

\begin{cases}
P(y=1|x;w)=f_w(x)& \\
P(y=0|x;w)=1-f_w(x)& \\
\end{cases}

mysql例子

 select
	*
from
	(
	select
		tb1.warId, tb1.beginTime, tb1.server, tb2.partyname, tb3.kandd, tb4.warpersoncount, tb5.maxkill, tb1.spaceline
	from
		(
		select
			DISTINCT warId, beginTime , server , CONCAT(name , ' ', lineid, '线') "spaceline"
		from
			(
			select
				distinct CONCAT(substring(server, 5, 4), ' ', LPAD(lineid, 2, 0), ' ', EXTRACT(YEAR FROM beginTime), ' ', LPAD(EXTRACT(month FROM beginTime), 2, 0), ' ', LPAD(EXTRACT(day from beginTime), 2, 0), ' ', LPAD(id, 4, 0)) "warId", server , mapId , beginTime , endTime, lineid
			from
				pss_ana.map_fight_info
			order by
				warId )tb1
		left join (
			select
				id, name
			from
				game_config.config_map_name )tb2 on
			tb1.mapId = tb2.id
		order by
			warId )tb1
	left join (
		select
			tbparty.warId, server, GROUP_CONCAT(tbparty.partyName SEPARATOR ',') partyname
		from
			(
			select
				DISTINCT warId, partyName, server
			from
				(
				select
					CONCAT(substring(server, 5, 4), ' ', LPAD(lineid, 2, 0), ' ', EXTRACT(YEAR FROM beginTime), ' ', LPAD(EXTRACT(month FROM beginTime), 2, 0), ' ', LPAD(EXTRACT(day from beginTime), 2, 0), ' ', LPAD(id, 4, 0)) "warId", killGuildId "partyId" , killGuildName "partyName", 'kill' as "state" , server
				from
					pss_ana.map_fight_info
			union all
				select
					CONCAT(substring(server, 5, 4), ' ', LPAD(lineid, 2, 0), ' ', EXTRACT(YEAR FROM beginTime), ' ', LPAD(EXTRACT(month FROM beginTime), 2, 0), ' ', LPAD(EXTRACT(day from beginTime), 2, 0), ' ', LPAD(id, 4, 0)) "warId" , dieGuildId "partyId" , dieGuildName "partyName", 'killed' as "state" , server
				from
					pss_ana.map_fight_info )tb1 )tbparty
		group by
			tbparty.warId, server )tb2 on
		tb1.warId = tb2.warId
		and tb1.server = tb2.server
	left join (
		select
			warId, group_concat(state SEPARATOR ',') "kandd", server
		from
			(
			select
				warId, concat('[', partyName, ']', state, ':', max(count)) "state" , server
			from
				(
				select
					warId, partyName, state, COUNT(1) as "count", server
				from
					(
					select
						warId, partyName, state, server
					from
						(
						select
							CONCAT(substring(server, 5, 4), ' ', LPAD(lineid, 2, 0), ' ', EXTRACT(YEAR FROM beginTime), ' ', LPAD(EXTRACT(month FROM beginTime), 2, 0), ' ', LPAD(EXTRACT(day from beginTime), 2, 0), ' ', LPAD(id, 4, 0)) "warId", killGuildId "partyId" , killGuildName "partyName", '杀人' as "state", server
						from
							pss_ana.map_fight_info
					union all
						select
							CONCAT(substring(server, 5, 4), ' ', LPAD(lineid, 2, 0), ' ', EXTRACT(YEAR FROM beginTime), ' ', LPAD(EXTRACT(month FROM beginTime), 2, 0), ' ', LPAD(EXTRACT(day from beginTime), 2, 0), ' ', LPAD(id, 4, 0)) "warId" , dieGuildId "partyId" , dieGuildName "partyName", '被杀' as "state", server
						from
							pss_ana.map_fight_info )tbparty
					order by
						warId, partyName )tbkill
				group by
					warId, partyName, state, server
			union all
				select
					DISTINCT warId, PartyName, '杀人' as "state", 0 as "count(1)", server
				from
					(
					select
						CONCAT(substring(server, 5, 4), ' ', LPAD(lineid, 2, 0), ' ', EXTRACT(YEAR FROM beginTime), ' ', LPAD(EXTRACT(month FROM beginTime), 2, 0), ' ', LPAD(EXTRACT(day from beginTime), 2, 0), ' ', LPAD(id, 4, 0)) "warId" , killGuildName "partyName", server
					from
						pss_ana.map_fight_info
				union all
					select
						CONCAT(substring(server, 5, 4), ' ', LPAD(lineid, 2, 0), ' ', EXTRACT(YEAR FROM beginTime), ' ', LPAD(EXTRACT(month FROM beginTime), 2, 0), ' ', LPAD(EXTRACT(day from beginTime), 2, 0), ' ', LPAD(id, 4, 0)) "warId" , dieGuildName "partyName", server
					from
						pss_ana.map_fight_info )tb1
			union all
				select
					DISTINCT warId, PartyName, '被杀' as "state", 0 as "count(1)", server
				from
					(
					select
						CONCAT(substring(server, 5, 4), ' ', LPAD(lineid, 2, 0), ' ', EXTRACT(YEAR FROM beginTime), ' ', LPAD(EXTRACT(month FROM beginTime), 2, 0), ' ', LPAD(EXTRACT(day from beginTime), 2, 0), ' ', LPAD(id, 4, 0)) "warId" , killGuildName "partyName", server
					from
						pss_ana.map_fight_info
				union all
					select
						CONCAT(substring(server, 5, 4), ' ', LPAD(lineid, 2, 0), ' ', EXTRACT(YEAR FROM beginTime), ' ', LPAD(EXTRACT(month FROM beginTime), 2, 0), ' ', LPAD(EXTRACT(day from beginTime), 2, 0), ' ', LPAD(id, 4, 0)) "warId" , dieGuildName "partyName", server
					from
						pss_ana.map_fight_info )tb1
				order by
					warId )tb1
			group by
				warId, partyName, state, server )tbkill1
		group by
			warId, server )tb3 on
		tb1.warId = tb3.warId
		and tb1.server = tb3.server
	left join (
		select
			warId, COUNT(DISTINCT id ) "warpersoncount", server
		from
			(
			select
				warId, id, server
			from
				(
				select
					CONCAT(substring(server, 5, 4), ' ', LPAD(lineid, 2, 0), ' ', EXTRACT(YEAR FROM beginTime), ' ', LPAD(EXTRACT(month FROM beginTime), 2, 0), ' ', LPAD(EXTRACT(day from beginTime), 2, 0), ' ', LPAD(id, 4, 0)) "warId", killId "Id", server
				from
					pss_ana.map_fight_info
			union all
				select
					CONCAT(substring(server, 5, 4), ' ', LPAD(lineid, 2, 0), ' ', EXTRACT(YEAR FROM beginTime), ' ', LPAD(EXTRACT(month FROM beginTime), 2, 0), ' ', LPAD(EXTRACT(day from beginTime), 2, 0), ' ', LPAD(id, 4, 0)) "warId" , dieId "Id" , server
				from
					pss_ana.map_fight_info )tbparty
			order by
				warId )tbczcount
		group by
			warId, server )tb4 on
		tb1.warId = tb4.warId
		and tb1.server = tb4.server
	left join (
		select
			warId, count(killid) "maxkill", server
		from
			(
			select
				CONCAT(substring(server, 5, 4), ' ', LPAD(lineid, 2, 0), ' ', EXTRACT(YEAR FROM beginTime), ' ', LPAD(EXTRACT(month FROM beginTime), 2, 0), ' ', LPAD(EXTRACT(day from beginTime), 2, 0), ' ', LPAD(id, 4, 0)) "warId", killid, server
			from
				pss_ana.map_fight_info )tbjsmax
		group by
			warId, server )tb5 on
		tb1.warId = tb5.warId
		and tb1.server = tb5.server )tball


pandas列名排序

df = df[ ['mean'] + [ col for col in df.columns if col != 'mean' ] ]

读取文件


import os
from os.path import isfile, join
import pandas as pd

path_ = "D:/dataset/competitive-data-science-predict-future-sales"

pos_files = [path_ + '/' + f for f in os.listdir(path_) if isfile(join(path_, f))]
print(len(pos_files))

unstack reset_index

将pd unstack后
通过reset_index来取数

根据某列来行化

dfld.groupby(['user','date'])['hour'].sum().unstack()
yt_consumption_location_df.groupby(['md5','route2'])['route2'].count().unstack().reset_index()

23小时52分16秒转秒数

def shushustr2sec(shushustr):
    day = checkNonelist(re.findall(r"(\d+)天",shushustr))
    hour = checkNonelist(re.findall(r"(\d+)小时",shushustr))
    minute= checkNonelist(re.findall(r"(\d+)分",shushustr))
    second = checkNonelist(re.findall(r"(\d+)秒",shushustr))
    #print(day,'\n',hour,'\n',minute,'\n',second)
    
    time = second + minute * 60 + hour *60*60 + day * 86400

    return time

堆叠图

import matplotlib.colors as mcolors

def pltbar(titlename,xlabel,label,botV,cenV,topV):
    plt.title(titlename)
    #plt.figure(figsize=(20,10))
    N = len(xlabel)
    ind = np.arange(N)  #[ 0  1  2  3  4  5  6  7  8 ]
    plt.xticks(ind, xlabel)

    # plt.ylabel('Scores')
    Bottom,Center,Top = botV,cenV,topV 


    d = []
    for i in range(0, len(Bottom)):
        sum = Bottom[i] + Center[i]
        d.append(sum)

    colors=list(mcolors.TABLEAU_COLORS.keys())
    
    p1 = plt.bar(ind, Bottom, color=colors[0]) 
    p2 = plt.bar(ind, Center, bottom=Bottom,color=colors[1])  
    p3 = plt.bar(ind, Top, bottom=d,color=colors[2])
    
    plt.legend((p1[0], p2[0], p3[0]),label,loc = 2)
    
    plt.show()
pltbar ('adad',['3','4','5'],['11','22','33'],[1,2,3],[3,4,5],[5,1,2])

在这里插入图片描述

多柱图

x = np.arange(size)
a = np.random.random(size)
b = np.random.random(size)
c = np.random.random(size)

# total_width, n = 0.8, 3
width = total_width / n
x = x - (total_width - width) / 2

plt.bar(x, a,  width=width, label='a')
plt.bar(x + width, b, width=width, label='b')
plt.bar(x + 2 * width, c, width=width, label='c')
plt.legend()

plt.plot(x,a)
plt.plot(x + width,b)
plt.plot(x + 2 * width, c)
# plt.plot(x, y, "r", marker='*', ms=10, label="a")
# plt.xticks(rotation=45)
# plt.legend(loc="upper left")

plt.show()

在这里插入图片描述

柱状折线图子图


from matplotlib.pyplot import MultipleLocator
# import matplotlib.figure as fig
fig=plt.figure()
plt.figure(figsize=(15,10)) 

labels = [str(x) for x in range(0,20)]

fig.tight_layout()#调整整体空白
plt.subplots_adjust(wspace =0.2, hspace =0.5)#调整子图间距


# plt.figure(12)
plt.subplot(421)
plt.ylim(0,max(pltalldata[0])+100)
plt.bar(range(len(pltalldata[0])), pltalldata[0],color=['dodgerblue'])
ax2 = plt.twinx()
plt.plot(range(len(pltratedata[0])), pltratedata[0] , "r", marker='.', ms=1)
# plt.xlim([str(x) for x in range(0,21)])
plt.title('day1')
x_major_locator=MultipleLocator(1)
ax2.xaxis.set_major_locator(x_major_locator)
plt.xlim(-0.5,20)
plt.ylim(0,100)

plt.subplot(422)
plt.ylim(0,max(pltalldata[1])+100)
plt.bar(range(len(pltalldata[1])), pltalldata[1],color=['dodgerblue'])
ax2 = plt.twinx()
plt.plot(range(len(pltratedata[1])), pltratedata[1] , "r", marker='.', ms=1)
# plt.xlim([str(x) for x in range(0,21)])
plt.title('day2')
x_major_locator=MultipleLocator(1)
ax2.xaxis.set_major_locator(x_major_locator)
plt.xlim(-0.5,20)
plt.ylim(0,100)


plt.subplot(423)
plt.ylim(0,max(pltalldata[2])+100)
plt.bar(range(len(pltalldata[2])), pltalldata[2],color=['dodgerblue'])
ax2 = plt.twinx()
plt.plot(range(len(pltratedata[2])),pltratedata[2] , "r", marker='.', ms=1)
# plt.xlim([str(x) for x in range(0,21)])
plt.title('day3')
x_major_locator=MultipleLocator(1)
ax2.xaxis.set_major_locator(x_major_locator)
plt.xlim(-0.5,20)
plt.ylim(0,100)

plt.subplot(424)
plt.ylim(0,max(pltalldata[3])+100)
plt.bar(range(len(pltalldata[3])), pltalldata[3],color=['dodgerblue'])
ax2 = plt.twinx()
plt.plot(range(len(pltratedata[3])), pltratedata[3], "r", marker='.', ms=1)
# plt.xlim([str(x) for x in range(0,21)])
plt.title('day4')
x_major_locator=MultipleLocator(1)
ax2.xaxis.set_major_locator(x_major_locator)
plt.xlim(-0.5,20)
plt.ylim(0,100)

plt.subplot(425)
plt.ylim(0,max(pltalldata[4])+100)
plt.bar(range(len(pltalldata[4])), pltalldata[4],color=['dodgerblue'])
ax2 = plt.twinx()
plt.plot(range(len(pltratedata[4])),pltratedata[4]  , "r", marker='.', ms=1)
# plt.xlim([str(x) for x in range(0,21)])
plt.title('day5')
x_major_locator=MultipleLocator(1)
ax2.xaxis.set_major_locator(x_major_locator)
plt.xlim(-0.5,20)
plt.ylim(0,100)

plt.subplot(426)
plt.ylim(0,max(pltalldata[5])+100)
plt.bar(range(len(pltalldata[5])), pltalldata[5],color=['dodgerblue'])
ax2 = plt.twinx()
plt.plot(range(len(pltratedata[5])), pltratedata[5] , "r", marker='.', ms=1)
# plt.xlim([str(x) for x in range(0,21)])
plt.title('day6')
x_major_locator=MultipleLocator(1)
ax2.xaxis.set_major_locator(x_major_locator)
plt.xlim(-0.5,20)
plt.ylim(0,100)

plt.subplot(427)


lastx = ['24', '48', '72', '96','120','144']
lasty = pltlastdata
lasty2 = pltolddata
plt.ylim(0,max(lasty)+100)
plt.bar(lastx,lasty,color=['dodgerblue'])
plt.bar(lastx,lasty2,color=['orange'])
for x1,y1 in zip(lastx,lasty):
    plt.text(x1, y1 + 1, str(y1), ha='center', va='bottom', fontsize=10, rotation=0)
for x1,y2 in zip(lastx,lasty2):
    plt.text(x1, y2 + 1, str(y2), ha='center', va='bottom', fontsize=10, rotation=0)
    
plt.title('pred num')
# plt.text(lastx,lasty+3,'%.3f'%lasty)
# x_major_locator=MultipleLocator(1)
# ax2.xaxis.set_major_locator(x_major_locator)
# plt.xlim(-0.5,6)

plt.subplot(428)
lastx = ['24', '48', '72', '96','120','144']
lasty = [round(x,2) for x in pltlastratedata]
plt.ylim(0,100)
plt.bar(lastx,lasty,color=['dodgerblue'])
for x1,y1 in zip(lastx,lasty):
    plt.text(x1, y1 + 1, str(y1), ha='center', va='bottom', fontsize=10, rotation=0)
plt.title('pred accuracy')
# plt.text(lastx,lasty+3,'%.3f'%lasty)
# x_major_locator=MultipleLocator(1)
# ax2.xaxis.set_major_locator(x_major_locator)
# plt.xlim(-0.5,6)

list提取中位数,25分位

def takepos(inputlist,pos):
    length = len(inputlist)
    pos_ = int(length * pos) -1
    inputlist.sort()
    ans = inputlist[pos_]
    return ans  

笛卡尔积相乘

def cartesian(l1, l2):
    carte = []
    for i in itertools.product(l1, l2):
        carte.append(i)
    df = pd.DataFrame(carte)
    return df

推荐系统重排

    op = []
    typelist = []
    for n in range(200):
        if len(typelist) >3:
            del(typelist[0])
        else:
            ndf = predf[~predf['goods_id'].isin(op)]
            ndf = ndf[~ndf['navigation_name_ch_set'].isin(typelist)]
            typelist.append(ndf.iloc[0][2])
            op.append(ndf.iloc[0][0])

打压

def pushlist(alist,blist,pushn):
    t = []
    for n in range(len(alist)):
        if alist[-(n+1)] in blist:
            t.insert(n - pushn,alist[-(n+1)])
        else:
            t.append(alist[-(n+1)])
    return t[::-1]

强推

def uplist(alist,blist,upn):
    t = []
    for n in range(len(alist)):
        if alist[n] in blist:
            t.insert(n - upn,alist[n])
        else:
            t.append(alist[n])
    return t

sql 字段名

select COLUMN_NAME from information_schema.COLUMNS where table_name = 'es_brand'

tqdm notebook

from tqdm import tqdm_notebook as tqdm

python解压

import zipfile
import os

# 压缩文件路径
zip_path='./open_clip-main.zip'
# 文件存储路径
save_path = './'

# 读取压缩文件
file=zipfile.ZipFile(zip_path)
# 解压文件
print('开始解压...')
file.extractall(save_path)
print('解压结束。')
# 关闭文件流
file.close()
# 修复编码

python压缩

import os
import zipfile
 

def MyFilezip(input_path,output_path):
    """
    压缩指定文件夹
    :param dirpath: 目标文件夹路径
    :param outFullName: 压缩文件保存路径+xxxx.zip
    :return: 无
    """
    
    zip = zipfile.ZipFile(output_path, "w", zipfile.ZIP_DEFLATED)
    for path, dirnames, filenames in os.walk(input_path):
        # 去掉目标跟路径,只对目标文件夹下边的文件及文件夹进行压缩
        fpath = path.replace(path, '')
 
        for filename in filenames:
            zip.write(os.path.join(path, filename), os.path.join(fpath, filename))
    zip.close()
 
 
if __name__ == "__main__":
    input_path = "./yyxq44_46/"
    output_path = "./yyxq44_46.zip"
 
    MyFilezip(input_path, output_path)
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值