Preserving Column Order - Python Pandas and Column Concat

自古美人都是妖i 提交于 2019-12-02 22:20:40

Assuming the concatenated DataFrame is df, you can perform the reordering of columns as follows:

important = ['Username', 'Name']
reordered = important + [c for c in df.columns if c not in important]
df = df[reordered]
print df

Output:

     Username   Name Alias1 Alias2
0  Tomfoolery    Tom    TJZ    NaN
1     MsMeryl  Meryl    Mer    NaN
2     Midsize  Timmy   Yoda    NaN
0    Firedbob    Bob   Fire  Gingy
1  Tomfoolery    Tom    TJZ   Awww

The list of numbers [0, 1, 2, 0, 1] is the index of the DataFrame. To prevent them from being written to the output file, you can use the index=False option in to_csv():

df.to_csv('Result.csv', index=False, sep=' ')
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!