![](/img/trans.png)
[英]How to set column headers while converting dict to pandas DataFrame (where column names do not match dict keys)?
[英]How to go from dictionary to pandas DataFrame to CSV while making the dict keys the first column, not the index
我希望字典的键是DataFrame的第一列 , 而不是index ,因为我需要这些值具有列名('ID')。 我一生无法弄清楚如何实现这一目标。
这是我的代码:
import pandas as pd
df = pd.DataFrame.from_dict(dict_name, orient='index', columns = ['A', 'B', 'C', 'D', 'E'])
df.to_csv('output_filename', sep='\t')
重置索引,然后在文件中不包括新生成的顺序索引:
df.index.names = ['ID']
df.reset_index().to_csv('output_filename', sep='\t', index=False)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.