dictionary = {'number': [2.1607142857142856, 1.5160021597156612, 1.0, 6.0],
'orbital_period': [847.5717667678571, 1796.522108459915, 0.73654, 14002.0],
'mass': [2.89239793814433, 4.4152433928281205, 0.0036, 21.42],
'distance': [39.760093457943924, 50.97704197461588, 1.35, 354.0],
'year': [2007.4732142857142, 5.01701576965303, 1995.0, 2013.0]}
zd = zip(*dictionary.values())
with open('file.csv', 'w') as file:
writer = csv.writer(file, delimiter=',')
writer.writerow(dictionary.keys())
writer.writerows(zd)如何回读此文件以查看是否获得了所需的输出?这会给我输出吗?
我希望输出在第一列中具有属性,并将数字列表作为行:
发布于 2020-06-09 09:23:46
这可能是一个观察熊猫的好时机。
import pandas as pd
dictionary = {'number': [2.1607142857142856, 1.5160021597156612, 1.0, 6.0],
'orbital_period': [847.5717667678571, 1796.522108459915, 0.73654, 14002.0],
'mass': [2.89239793814433, 4.4152433928281205, 0.0036, 21.42],
'distance': [39.760093457943924, 50.97704197461588, 1.35, 354.0],
'year': [2007.4732142857142, 5.01701576965303, 1995.0, 2013.0]}
df = pd.DataFrame.from_dict(dictionary)
df.to_csv('output.csv', index=False)再读一遍
df = pd.read_csv('output.csv')数据帧将如下所示
number orbital_period mass distance year
0 2.160714 847.571767 2.892398 39.760093 2007.473214
1 1.516002 1796.522108 4.415243 50.977042 5.017016
2 1.000000 0.736540 0.003600 1.350000 1995.000000
3 6.000000 14002.000000 21.420000 354.000000 2013.000000https://stackoverflow.com/questions/62273541
复制相似问题