key value 为值,列
import pandas as pd
data = {'a': 1, 'b': 2, 'c': 3, 'd': 4}
def save_to_excel(data):
"""
将字典数据存入Excel
"""
pf = pd.DataFrame()
# 设置列 值 表头
pf['name'] = list(data.keys())
pf['value'] = list(data.values())
save_path = r'G:\Project\zxp\111.xlsx'
# index 为FALSE 表示没有第一列数字为索引
pf.to_excel(save_path, encoding='utf-8', index=False)
save_to_excel(data)
import pandas as pd
def save_key_value():
"""
将字典数据存入Excel
"""
data = [{'a': 1, 'b': 2, 'c': 3, 'd': 4}, {'a': 1, 'b': 2, 'c': 3, 'd': 4}]
pf = pd.DataFrame(data)
# 数据在列表中的表头顺序
order = ['b', 'c', 'd', 'a']
pf = pf[order]
save_path = r'G:\Project\zxp\112.xlsx'
# index 为FALSE 表示没有第一列数字为索引
pf.to_excel(save_path, encoding='utf-8', index=False, header=True)
save_key_value()
import pandas as pd
def save_list():
"""
将列表数据存入Excel
"""
data1 = ['a', 'b', 'c', 'd', 'a', 1, 'b', 2, 'c', 3, 'd', 4]
data2 = ['a', 'b', 'c', 'd', 'a', 1, 'b', 2, 'c', 3, 'd', 4]
pf = pd.DataFrame()
# 设置列 值 表头
pf['name'] = data1
pf['value'] = data2
save_path = r'G:\Project\zxp\113.xlsx'
# index 为FALSE 表示没有第一列数字为索引
pf.to_excel(save_path, encoding='utf-8', index=False, header=True)
save_list()