在 Pandas 数据框内移动列

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33216180/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 00:04:31  来源:igfitidea点击:

Move columns within Pandas DATA FRAME

pythonpandasipython

提问by puk789

I have read data from csv file into a data frame consisting of more than 25000 rows and 15 columns and I need to move all rows (including the left-most -> index) one column to the right, so that I get an empty index and be able to fill it with integers. Names of columns, however, should stay at the same place. So, basically I need to move everything except column names one place to the right.

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

enter image description here

在此处输入图片说明

I tried to reindex it, but got an error:

我试图重新索引它,但出现错误:

ValueError: cannot reindex from a duplicate axis

Is there any way to do this?

有没有办法做到这一点?

回答by Leb

In pandas you can only create a column to the right, unless you do join between two dataframe. Then you can re-arrange however you like.

在 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

回答by Fabio Lamanna

I would first add a new column with:

我会首先添加一个新列:

df['new'] = df.index

than take the names of the columns of your dataframe in a list with:

而不是在列表中获取数据框列的名称:

colnames = df.columns.tolist()

Then you can rearrange them as you need, for instance changing the order so you get the last 'new' column as first and move the remains one position to the right:

然后您可以根据需要重新排列它们,例如更改顺序,以便您首先获得最后一个“新”列,并将剩余的一个位置向右移动:

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

and reassign:

并重新分配:

df = df[colnames]

回答by Pedro Bronze

You could use .shift()method in order to roll dataframe values both column/row-wise by an integer value.

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

Not exactly your case but you could find a few usage cases here for the method : Shift column in pandas dataframe up by one?

不完全是您的情况,但您可以在此处找到该方法的一些使用案例:将 Pandas 数据框中的列向上移动一个?

I observed that

我观察到

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

would drop the values in the index column and turn them into NaN.

将删除索引列中的值并将它们转换为 NaN。

A workaround would be:

一种解决方法是:

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

It's quite a bit of code, but I think would fit the purpose.

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

Edit: We could avoid creating the variable 'values' and make it in two lines with:

编辑:我们可以避免创建变量 'values' 并将其分成两行:

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

回答by Snowde

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)