简体   繁体   English

如何将Python词典保存到没有索引的csv文件中?

[英]How to save Python dictionary to csv file without index?

I stored 3 DataFrame in a dictionary called dict , and I would like to save all the dataframes in a csv file like this, 我将3个DataFrame存储在名为dict ,我想将所有数据帧保存在这样的csv文件中,

      dict 
      Key  Type        Size    Value
      001  DataFrame   (3,4)   Columns names: date, count, number, expiration
      002  DateFrame   (3,4)   Columns names: date, count, number, expiration               
      003  DateFrame   (3,4)   Columns names: date, count, number, expiration

      Index      date     count  number    expiration
       0      1991-10-01    3     001      1992-10-01
       1      1991-10-02    5     002      1992-10-02
       2      1991-10-03    7     003      1992-10-03

I tried, 我试过了,

with open('data.csv','w') as f:
     w = csv.writer(f, delimiter = '\t')
     w.writerows(dict.items())

But it turns out like this, one row with all the values and also Index 但结果是这样,一行包含所有值以及索引 在此处输入图片说明

You can youse to_csv function of class DataFrame : 您可以使用to_csv类的DataFrame函数:

df.to_csv('/path/to/file.csv',sep='\t')

(assuming your DataFrame is named df ) (假设您的DataFrame名为df


UPDATE: if you have multiple DataFrames of the same type then you can merge them in one DataFrame (instead of keeping them in a dictionary separately) and use to_csv function to store it. 更新:如果您有多个相同类型的DataFrame,则可以将它们合并到一个DataFrame中(而不是将它们分别保存在字典中),然后使用to_csv函数存储它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM