pandas 移动数据框列并更改列顺序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/50660949/
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 05:38:21  来源:igfitidea点击:

Moving a dataframe column and changing column order

pythonpandasdataframe

提问by Stacey

I have a dataframe called dfwhich has the following columns header of data:

我有一个名为的数据框df,它具有以下列数据标题:

date           A    B     C   D    E    F      G          H       I
07/03/2016  2.08    1   NaN NaN 1029    2   2.65    4861688 -0.0388
08/03/2016  2.20    1   NaN NaN 1089    2   2.20    5770819 -0.0447
:                                                                 :   

09/03/2016  2.14    1   NaN NaN 1059    2   2.01    5547959 -0.0514
10/03/2016  2.25    1   NaN NaN 1089    2   1.95    4064482 -0.0520

Is there a way to change the order of the columns so that column F is moved to a position that is after column H. The resulting dfwould look like:

有没有办法更改列的顺序,以便将 F 列移动到 H 列之后的位置。结果df如下所示:

date           A    B     C   D    E    F      G          H  F       I
07/03/2016  2.08    1   NaN NaN 1029    2   2.65    4861688  2 -0.0388
08/03/2016  2.20    1   NaN NaN 1089    2   2.20    5770819  2 -0.0447
:                                                                    :   

09/03/2016  2.14    1   NaN NaN 1059    2   2.01    5547959  2 -0.0514
10/03/2016  2.25    1   NaN NaN 1089    2   1.95    4064482  2 -0.0520

采纳答案by asapo kL

Use this :

用这个 :

df = df[['date','A','B','C','D','E','F','G','H','F','I']]

--- Edit

- - 编辑

columnsName = list(df.columns)
F, H = columnsName.index('F'), columnsName.index('H')
columnsName[F], columnsName[H] = columnsName[H],columnsName[F]
df = df[columnsName]

回答by cs95

Use df.insertwith df.columns.get_locto dynamically determine the position of insertion.

使用df.insertwithdf.columns.get_loc动态确定插入的位置。

col = df['F'] # df.pop('F') # if you want it removed
df.insert(df.columns.get_loc('H') + 1, col.name, col, allow_duplicates=True)

df
         date     A  B   C   D     E  F     G        H  F       I
0  07/03/2016  2.08  1 NaN NaN  1029  2  2.65  4861688  2 -0.0388
1  08/03/2016  2.20  1 NaN NaN  1089  2  2.20  5770819  2 -0.0447
...

回答by k.ko3n

Not for the author of this question, but perhaps for others.

不是针对这个问题的作者,而是针对其他人。

list = df.columns.tolist() # list the columns in the df
list.insert(8, list.pop(list.index('F'))) # Assign new position (i.e. 8) for "F" 
df = df.reindex(columns= list) # Now move 'F' to ist new position

回答by jpp

This is one way via pd.DataFrame.iloc, which uses integer-location based indexing for selecting by position.

这是 via 的一种方式pd.DataFrame.iloc,它使用基于整数位置的索引来按位置进行选择。

It's also a gentle reminder that pandasinteger indexing is based on numpy.

这也是一个温和的提醒,pandas整数索引是基于numpy.

import pandas as pd
import numpy as np

df = pd.DataFrame(columns=list('ABCDEFGHI'))

cols = np.insert(np.arange(df.shape[1]),
                 df.columns.get_loc('H')+1,
                 df.columns.get_loc('F'))

res = df.iloc[:, cols]

print(res)

Empty DataFrame
Columns: [A, B, C, D, E, F, G, H, F, I]
Index: []

回答by llllllllll

You can use:

您可以使用:

df.reindex(['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'F', 'I'], axis=1)