pandas 查询写入

cooolr 于 2022-08-24 发布

安装

pip install pandas

写入csv

import pandas as pd

df=pd.DataFrame([["A", "B", "C", "D", "E", "F", "G"],[1,2,3,4,5,6,7],[7,6,5,4,3,2,1]])

df.to_csv('Result1.csv', index=None,header=None, encoding="utf-8")

写入xlsx

import pandas as pd

df=pd.DataFrame([["A", "B", "C", "D", "E", "F", "G"],[1,2,3,4,5,6,7],[7,6,5,4,3,2,1]])

writer = pd.ExcelWriter("Result.xlsx", engine = "xlsxwriter")

df.to_excel(writer, index=None, header=None, encoding="utf-8")

writer.save()

写入多个sheet

import pandas as pd

df1=pd.DataFrame([["A", "B", "C", "D", "E", "F", "G"],[1,2,3,4,5,6,7],[7,6,5,4,3,2,1]])
df2=pd.DataFrame([["A", "B", "C", "D", "E", "F", "G"],[1,2,3,4,5,6,7],[7,6,5,4,3,2,1]])

writer = pd.ExcelWriter('output.xlsx')
df1.to_excel(writer,'Sheet1',index=None,header=None, encoding="utf-8")
df2.to_excel(writer,'Sheet2',index=None,header=None, encoding="utf-8")
writer.save()

group by

valid_data.groupby(valid_data["label"]).size()

读取excel

df = pd.read_excel("2017-12-31 ~ 2018-12-31.xlsx")
news_id_list = df["id"].tolist()

遍历

for i in list(df.itertuples())[1:]:
    print(i)
    break