在Pandas DATA FRAME中移动列

时间:2015-10-19 13:48:25

标签: python pandas ipython

我已经将csv文件中的数据读入包含超过25000行和15列的数据框中,我需要将所有行(包括最左边的>索引)向右移动一列,这样我就可以了得到一个空索引,并能够用整数填充它。但是,列的名称应保持在同一位置。所以,基本上我需要将除列名之外的所有内容移到右边一个位置。

enter image description here

我尝试重新编制索引,但收到了错误:

ValueError: cannot reindex from a duplicate axis

有没有办法做到这一点?

4 个答案:

答案 0 :(得分:10)

在pandas中,您只能在右侧创建一列,除非您在两个数据帧之间进行连接。然后你可以随心所欲地重新安排。

import pandas as pd

df = pd.read_csv('data.csv', header=None, names = ['A','B','C'])

print(df)

    A   B   C
0   1   2   3
1   4   5   6
2   7   8   9
3  10  11  12

df['D'] = pd.np.nan # this creates an empty series
                    # and appends to the right

print(df)

    A   B   C   D
0   1   2   3 NaN
1   4   5   6 NaN
2   7   8   9 NaN
3  10  11  12 NaN

df = df[['D','A','B','C']] # rearrange as you like

print(df)

    D   A   B   C
0 NaN   1   2   3
1 NaN   4   5   6
2 NaN   7   8   9
3 NaN  10  11  12

答案 1 :(得分:4)

我首先要添加一个新列:

df['new'] = df.index

而不是使用以下列表获取数据框中列的名称:

colnames = df.columns.tolist()

然后您可以根据需要重新排列它们,例如更改顺序,这样您就可以将最后一个“新”列作为第一个,并将剩余的一个位置移到右侧:

colnames = colnames[-1:] + colnames[:-1]

并重新分配:

df = df[colnames]

答案 2 :(得分:1)

您可以使用.shift()方法将数据帧值按列/行方向滚动一个整数值。

并非完全符合您的情况,但是您可以在此处找到该方法的一些使用案例:Shift column in pandas dataframe up by one?

我观察到

df.reset_index().shift(1,axis=1)

将这些值放在索引列中并将其转换为NaN。

一种解决方法是:

df = df.reset_index()
values = df.iloc[:,0].values
df = df.shift(1,axis=1)
df.iloc[:,1] = values

这是很多代码,但我认为符合目的。

编辑: 我们可以避免创建变量“值”,并通过以下两行使其变为:

new_df = df.reset_index().shift(1,axis=1)
new_df.iloc[:,1] = df.reset_index().values[:,0]

答案 3 :(得分:0)

df = YourDataFrame
col = "Your Column You Want To Move To The Start Of YourDataFrame" 
df = pd.concat([df[col],df.drop(col,axis=1)], axis=1)
相关问题