简体   繁体   中英

Move specific columns to the rightmost of the DataFrame

I want to shift some columns in the middle of the dataframe to the rightmost. I could do this with individual column using code:

cols=list(df.columns.values)
cols.pop(cols.index('one_column'))
df=df[cols +['one_column']]
df

But it's inefficient to do it individually when there are 100 columns of 2 series, ie. series1_1... series1_50 and series2_1... series2_50 in the middle of the dataframe.

How can I do it by assigning the 2 series as lists, popping them and putting them back? Maybe something like

cols=list(df.columns.values)
series1 = list(df.loc['series1_1':'series1_50'])
series2 = list(df.loc['series2_1':'series2_50'])
cols.pop('series1', 'series2')
df=df[cols +['series1', 'series2']]

but this didn't work. Thanks

If you just want to shift the columns, you could call concat like this:

cols_to_shift = ['colA', 'colB']

pd.concat([
    df[df.columns.difference(cols_to_shift)],
    df[cols_to_shift]
  ], axis=1
)

Or, you could do a little list manipulation on the columns.

cols_to_keep = [c for c in df.columns if c not in cols_to_shift]
df[cols_to_keep + cols_to_shift]

Minimal Example

np.random.seed(0)
df = pd.DataFrame(np.random.randint(1, 10, (3, 5)), columns=list('ABCDE'))
df

   A  B  C  D  E
0  6  1  4  4  8
1  4  6  3  5  8
2  7  9  9  2  7

cols_to_shift = ['B', 'C']
pd.concat([
    df[df.columns.difference(cols_to_shift)],
    df[cols_to_shift]
  ], axis=1
)

   A  D  E  B  C
0  6  4  8  1  4
1  4  5  8  6  3
2  7  2  7  9  9

[c for c in df.columns if c not in cols_to_shift] 
df[cols_to_keep + cols_to_shift]

   A  D  E  B  C
0  6  4  8  1  4
1  4  5  8  6  3
2  7  2  7  9  9

I think list.pop only takes indices of the elements in the list. You should list.remove instead.

cols = df.columns.tolist()
for s in (‘series1’, ‘series2’):
    cols.remove(s)
df = df[cols + [‘series1’, ‘series2’]]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM