[英]How to go from dictionary to pandas DataFrame to CSV while making the dict keys the first column, not the index
I want the keys of the dictionary to be the first column of the DataFrame, not the index , because I need those values to have a column name ('ID'). 我希望字典的键是DataFrame的第一列 , 而不是index ,因为我需要这些值具有列名('ID')。 I cannot for the life of me figure out how to make that happen.
我一生无法弄清楚如何实现这一目标。
Here is my code: 这是我的代码:
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')
Reset the index then don't include the newly generated sequential index in the file: 重置索引,然后在文件中不包括新生成的顺序索引:
df.index.names = ['ID']
df.reset_index().to_csv('output_filename', sep='\t', index=False)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.