跳转至
本文阅读量

1. Pandas 用法

1.1 数据读取

1.1.1 快速从 CSV/Excel 中读取数据

df = pd.read_excel('example-1.xlsx')
data1 = json.loads(df.to_json(orient='records'))

df = pd.read_csv('example-2.csv')
data2 = json.loads(df.to_json(orient='records'))

1.2 保存

1.2.1 多个sheet写入到同一个Excel

import pandas as pd

df1 = pd.DataFrame({'One': [1, 2, 3]})
df2 = pd.DataFrame({'Two': [4, 5, 6]})

with pd.ExcelWriter('excel1.xlsx') as writer:
    df1.to_excel(writer, sheet_name='Sheet1', index=False)
    df2.to_excel(writer, sheet_name='Sheet2', index=False)

1.2.2 新增sheet,不覆盖已存在的sheet

import pandas as pd

df3 = pd.DataFrame({'Three': [7, 8, 9]})
with pd.ExcelWriter('excel1.xlsx', mode='a') as writer:
    df3.to_excel(writer, sheet_name='Sheet3', index=False)

import pandas as pd

df4 = pd.DataFrame({'Four': [11, 22, 33]})
df5 = pd.DataFrame({'Five': [55, 66, 77]})

with pd.ExcelWriter('excel1.xlsx', mode='a') as writer:
    df4.to_excel(writer, sheet_name='Sheet4', index=False)
    df5.to_excel(writer, sheet_name='Sheet5', index=False)

1.3 参考