python常用函数 画图matplotlib pandas dataframe 文件操作 json numpy

python常用函数 画图matplotlib pandas dataframe 文件操作 json numpy 字符串操作FLIP in IMG_0301_FLIP.jpg # true文件操作tgt os.path.join(str1, str2, str3) tgt glob.glob(path) # 返回path包含的文件名list(不递归), 返回的路径类型与tgt有关 tgt.sort() os.path.basename(绝对路径) # 返回文件名 os.listdir(path) # 返回文件名list shutil.copy(src, dst) shutil.rmtree(dir_path) os.remove(path) os.unlink(path) os.rename(old, new) shutil.move(oldpath, newpath)matplotlib# 显示图片 matplotlib.use(Qt5Agg) plt.imshow(image_np) plt.show() # 画折线图 x [i for i in range(1, 51)] y1 [i for i in range(1, 51)] y2 [i for i in range(51, 101)] plt.scatter(x, y, colorred, s5) # 画散点图 s是点大小 plt.plot(x, y1, labelline1, colorred, lw1) # 画折线图 plt.plot(x, y2, labelline2, colorblue, lw1) # linewidth plt.pie() # 画饼状图 plt.title(str) plt.xlabel(x) plt.ylabel(y) # 图例 plt.legend(locupper left) plt.show()jsonjson.dumps({ # 将对象转换为json字符串 JSON.stringify total: total, res: res, }) json.loads(str) # 将json字符串转换为对象 JSON.parsetimetime.time() # 1767969560.242972pandas polarsdf的所有method都不进行原地修改import pandas as pd pd.set_option(display.width, 0) pd.set_option(display.max_rows, None) pd.set_option(display.max_columns, None) df pd.read_excel(path) df pd.read_csv(path) df.to_excel(path, indexFalse) df[[col1, col2, col3]] # 选取其中的列 df.loc[0, col] # 选取多行多列 df.head(10) # 首10行 df.tail(20) df.drop_duplicates([NOC, Year, Sport]) df[new] df[old1] df[old2] # 对整列进行操作 len(df) # df的行数 newdf df.copy() # 深拷贝 for idx, row in df.iterrows(): # 按行遍历 pass df[df[cur].isin([a, b])] # 按某列的值筛选 df[(df[col1]1) (df[col2]2)] # 筛选 df.groupby([col1, col2, col3]) # 分组 返回DFGroup类 df.groupby([col1, col2, col3]).size() # 聚合函数, 返回Series series.to_frame(namecount) df.reset_index(dropTrue) # 重置idx, 删除原来idx pd.merge(df1, df2, on[NOC, Year], howleft) # 进行合并 df df.merge( df2[[col1, col2, col3]], left_on [col1], right_on [col1], suffixes(, 1), howleft, ) df.columnsimport polars as plpython单例模式import threading class Tester: _lock threading.Lock() _instance None def __new__(cls, *args, **kwargs): if not cls._instance: with cls._lock: if not cls._instance: cls._instance super().__new__(cls) return cls._instancepython爬虫import requests from time import sleep url https://wrds-www.wharton.upenn.edu/search/company-search/code-lookup/ headers {} data response requests.post( url url, headersheaders, data data, proxies proxy, ) json response.json() sleep(2)python连接MySQLMySQL :: MySQL Connector/Python Developer Guide :: 5 Connector/Python Coding ExamplesnumpyA np.array([[1, 2, 3], [4, 5, 6]]) B np.array([[1], [2], [3]]) C np.dot(A, B) # 矩阵乘法