【Python】pandas 读取,保存数据

pandas 读取/保存数据

import pandas as pd

file = r''
df = pd.read_excel(file)
df_columns = df.columns.to_list()  # 字段名list

redundant_column = ['name', 'age']  # 不需要的数据列
df.drop(labels=redundant_column, axis=1, inplace=True)  # 删除不需要的数据列

column_contrast_field = {
	'污染物名称': 'name',
}
df.rename(columns=column_contrast_field, inplace=True)  # 重命名字段名

res_data = df.to_dict(orient='records')  # 将数据转成 字典形式列表, 每行一个字典
res_data = df.to_dict(orient='list')  # 将数据转成 字典形式, 每列一个列表

df.to_excel(save_file, index=False)  # 保存数据到 excel

posted @ 2023-01-06 10:49  是阿杰呀  阅读(454)  评论(0)    收藏  举报