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


186

我可以在熊猫的特定列索引处插入列吗?

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

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


在DataFrame的开头(最左端)插入一列 -更多解决方案+通用解决方案,用于插入任何序列(不仅仅是常量值)。
cs95 '19

Answers:


363

参见文档:http : //pandas.pydata.org/pandas-docs/stable/genic/pandas.DataFrame.insert.html

使用loc = 0将在开头插入

df.insert(loc, column, 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

18
对于将来的用户,新参数是“ loc”,“ column”“ value”消息来源
Peter Maguire

11

您可以尝试将列提取为列表,根据需要对其进行按摩,然后为数据框重新编制索引:

>>> 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

编辑:这可以在一行中完成;但是,这看起来有点难看。也许会有更清洁的建议...

>>> 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

9

如果要为所有行使用一个值:

df.insert(0,'name_of_column','')
df['name_of_column'] = value

编辑:

你也可以:

df.insert(0,'name_of_column',value)

0

这是一个非常简单的答案(仅一行)。

在将“ n”列添加到df中之后,您可以按照以下步骤进行操作。

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

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

# here you can add the below code and it should work.
df = df[list('nlv')]
df

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



However, if you have words in your columns names instead of letters. It should include two brackets around your column names. 

import pandas as pd
df = pd.DataFrame({'Upper':['a','b','c','d'], 'Lower':[1,2,1,2]})
df['Net'] = 0
df['Mid'] = 2
df['Zsore'] = 2

df

    Upper   Lower   Net Mid Zsore
0   a       1       0   2   2
1   b       2       0   2   2
2   c       1       0   2   2
3   d       2       0   2   2

# here you can add below line and it should work 
df = df[list(('Mid','Upper', 'Lower', 'Net','Zsore'))]
df

   Mid  Upper   Lower   Net Zsore
0   2   a       1       0   2
1   2   b       2       0   2
2   2   c       1       0   2
3   2   d       2       0   2
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.