【发布时间】:2019-04-02 08:57:23
【问题描述】:
我正在尝试将一组 csv 文件合并到一个 Dataframe 中。在此过程中,我创建了一个名为 Time_Created 的新列,我试图将其作为 Dataframe 的第一列。
df_v1 = pd.concat([pd.read_csv(f) for f in updatedfiles_1], sort=True)
cols = df_v1.columns.tolist()
print(cols)
cols.insert(0, cols.pop(cols.index('Time_Created')))
print(cols) <-- This shows the columns as expected
df_v1.to_csv('file.csv')
我看到在保存到 csv 之前打印列时,列会根据需要进行修改,但是当我打开保存的 csv 时,列排序会发生变化。
下面给出的是源中的列序列:
Name,Price,Quanity,Time_Created
我要排序的序列:
Time_Created,Name,Price,Quanity
谁能指导我为什么输出文件会更改列排序。谢谢。
【问题讨论】:
-
修改
cols后是否尝试设置df_v1.columns = cols?
标签: python-3.x pandas columnsorting