发布于 

😰Python

This is a python test

配置

python -m pip install pandas ——安装模块

实用代码

数据处理

文件夹

返回当前工作目录——os.getcwd()

表格

ex = pd.ExcelFile(file)——ex.sheet_names()——读取sheet
data= pd.read_excel(ex,sheet,index_col)——读取文件
df1=pd.read_csv(“.\ppi-test-8860\A_B\ppi_nodes.xls”,sep=“\t”)——读取xls文件
data.columns——列名
data.index——行索引
data.loc[:,[“”,“”]]——提取包含列
data.iloc[0, 0]——取第几行几列
df = df.assign(column_new_1=np.nan, column_new_2=‘dogs’, column_new_3=3)——添加多列

数据转化

二维列表转化为一维列表——listA=[x for j in listB for x in j]
set类型——排除重复元素
统计个数——Counter(la)–from collections import Counter
数组形式——data.items()
排序——dpro = sorted(dproscore.items(), key=lambda x: x[1])–从小到大
将表转化为字典——dict(zip(df.iloc[:,0],df.iloc[:,1]))
创建新对象赋值——a.copy()
一行形式的for循环——[x for x in a]

字符处理

使用正则表达式进行匹配——re.search(“0-9”,line)
将字典的一个键对应的多个值前都添加相同的字符——dic_g[i] = [org + str(x) for x in dic_g[i]]
rstrip——去除末尾字符串
匹配特定文件后缀——[x for x in a if re.search(r".py$“,x)]
列表替换—[x.replace(“path:”,”") for x in a]

图片绘制

图例紧随其后——savefig(‘{compare}.pdf’.format(compare=name),dpi=300,bbox_inches=‘tight’)
设置画板背景——plt.figure(figsize=(7, 5.6),facecolor=‘white’)—

爬虫

爬取数据——response=requests.get(url, headers=self.headers)
url—网址
headers—response.status_code == 200—响应正常
response.text—html格式的文本形式

其他

字符串前加 f 的意思格式化 {} 内容,不在 {} 内的照常展示输出,如果你想输出 {},那就用双层 {{}} 将想输出的内容包起来。