Python:将行插入数据框

时间:2014-09-19 23:32:48

标签: python pandas

是否有更多pythonic方法将行插入数据框?我觉得这必须是大熊猫的功能,但找不到它。特别是,有没有办法“重置”指数?

谢谢。

data = {'state': ['Ohio', 'Ohio', 'Ohio', 'Nevada', 'Nevada'],
    'year': [2000, 2001, 2002, 2001, 2002],
    'pop': [1.5, 1.7, 3.6, 2.4, 2.9]}
frame = pd.DataFrame(data)

new =  pd.DataFrame(np.zeros(len(frame.columns)).reshape(1,len(frame.columns)),columns=frame.columns)
row = 3
def insert_row(frame,new,row):
    top = frame[0:row]
    bottom = frame[row:]
    return pd.concat((top,new,bottom))

但是,运行以上命令会返回:

   pop   state  year
0  1.5    Ohio  2000
1  1.7    Ohio  2001
2  3.6    Ohio  2002
0  0.0       0     0
3  2.4  Nevada  2001
4  2.9  Nevada  2002

2 个答案:

答案 0 :(得分:0)

使用loc属性分配数据。语法是df.loc[row_index, col_index]。一个例子:

import pandas
df = pandas.DataFrame(index=list('abcd'), columns=list('ABCD'))
df.loc['a', 'A'] = 1 # re-assigns an existing item
df.loc['b'] = [1, 2, 3, 4] # set the whole row
df.loc['e', 'E'] = 5 # creates a new row and column
df = df.reindex(list('abecd'))
print(df)

     A    B    C    D   E
a    1  NaN  NaN  NaN NaN
b    1    2    3    4 NaN
e  NaN  NaN  NaN  NaN   5
c  NaN  NaN  NaN  NaN NaN
d  NaN  NaN  NaN  NaN NaN

答案 1 :(得分:0)

如果您当前的功能对您有效,我建议您只需在返回的结果中添加reset_index即可。见下面的内容:

    ...:     return pd.concat((top,new,bottom)).reset_index(drop=True)

In [17]: new_frame = insert_row(frame, new, row)

In [18]: new_frame
Out[18]: 
   pop   state  year
0  1.5    Ohio  2000
1  1.7    Ohio  2001
2  3.6    Ohio  2002
3  0.0       0     0
4  2.4  Nevada  2001
5  2.9  Nevada  2002

参数drop=True强制它删除旧索引。将其保留在False会将旧索引保留为新的第一列。