如何在pandas中的特定列索引处插入列?

时间:2021-07-22 21:23:46

Can I insert a column at a specific column index in pandas?

我可以在pandas中的特定列索引处插入列吗?

import pandas as pd
df = pd.DataFrame({'l':['a','b','c','d'], 'v':[1,2,1,2]})
df['n'] = 0

This will put column n as the last column of df, but isn't there a way to tell df to put n at the beginning?

这会将列n作为df的最后一列,但是有没有办法告诉df将n放在开头?

2 个解决方案

#1


183  

see docs: http://pandas.pydata.org/pandas-docs/stable/dsintro.html#column-selection-addition-deletion

见文档:http://pandas.pydata.org/pandas-docs/stable/dsintro.html#column-selection-addition-deletion

using idx = 0 will insert at the beginning

使用idx = 0将在开头插入

df.insert(idx, col_name, value)

df = pd.DataFrame({'B': [1, 2, 3], 'C': [4, 5, 6]})

df
Out: 
   B  C
0  1  4
1  2  5
2  3  6

idx = 0
new_col = [7, 8, 9]  # can be a list, a Series, an array or a scalar   
df.insert(loc=idx, column='A', value=new_col)

df
Out: 
   A  B  C
0  7  1  4
1  8  2  5
2  9  3  6

#2


6  

You could try to extract columns as list, massage this as you want, and reindex your dataframe:

您可以尝试将列提取为列表,根据需要按下此按钮,然后重新索引数据框:

>>> cols = df.columns.tolist()
>>> cols = [cols[-1]]+cols[:-1] # or whatever change you need
>>> df.reindex(columns=cols)

   n  l  v
0  0  a  1
1  0  b  2
2  0  c  1
3  0  d  2

EDIT: this can be done in one line ; however, this looks a bit ugly. Maybe some cleaner proposal may come...

编辑:这可以在一行中完成;然而,这看起来有点难看。也许一些更清洁的提案可能来......

>>> df.reindex(columns=['n']+df.columns[:-1].tolist())

   n  l  v
0  0  a  1
1  0  b  2
2  0  c  1
3  0  d  2

#1


183  

see docs: http://pandas.pydata.org/pandas-docs/stable/dsintro.html#column-selection-addition-deletion

见文档:http://pandas.pydata.org/pandas-docs/stable/dsintro.html#column-selection-addition-deletion

using idx = 0 will insert at the beginning

使用idx = 0将在开头插入

df.insert(idx, col_name, value)

df = pd.DataFrame({'B': [1, 2, 3], 'C': [4, 5, 6]})

df
Out: 
   B  C
0  1  4
1  2  5
2  3  6

idx = 0
new_col = [7, 8, 9]  # can be a list, a Series, an array or a scalar   
df.insert(loc=idx, column='A', value=new_col)

df
Out: 
   A  B  C
0  7  1  4
1  8  2  5
2  9  3  6

#2


6  

You could try to extract columns as list, massage this as you want, and reindex your dataframe:

您可以尝试将列提取为列表,根据需要按下此按钮,然后重新索引数据框:

>>> cols = df.columns.tolist()
>>> cols = [cols[-1]]+cols[:-1] # or whatever change you need
>>> df.reindex(columns=cols)

   n  l  v
0  0  a  1
1  0  b  2
2  0  c  1
3  0  d  2

EDIT: this can be done in one line ; however, this looks a bit ugly. Maybe some cleaner proposal may come...

编辑:这可以在一行中完成;然而,这看起来有点难看。也许一些更清洁的提案可能来......

>>> df.reindex(columns=['n']+df.columns[:-1].tolist())

   n  l  v
0  0  a  1
1  0  b  2
2  0  c  1
3  0  d  2