df = pd.DataFrame({'a':[1,2,3,4],'b':[5,6,7,8],'c':[9,10,11,12]})
如何在索引0
的一行中插入一行新的零? 我试过pd.concat([pd.DataFrame([[0,0,0]]),df)
但它没有工作。如何将行添加到pandas中的现有数据框中? - python
所需的输出:
a b c
0 0 0 0
1 1 5 9
2 2 6 10
3 3 7 11
4 4 8 12
df = pd.DataFrame({'a':[1,2,3,4],'b':[5,6,7,8],'c':[9,10,11,12]})
如何在索引0
的一行中插入一行新的零? 我试过pd.concat([pd.DataFrame([[0,0,0]]),df)
但它没有工作。如何将行添加到pandas中的现有数据框中? - python
所需的输出:
a b c
0 0 0 0
1 1 5 9
2 2 6 10
3 3 7 11
4 4 8 12
可以Concat的临时DF与原来的DF但你需要,使其在级联DF对准通过相同的列名,还获得指数为你愿望呼吁reset_index
与drop=True
参数。
In [87]:
pd.concat([pd.DataFrame([[0,0,0]], columns=df.columns),df]).reset_index(drop=True)
Out[87]:
a b c
0 0 0 0
1 1 5 9
2 2 6 10
3 3 7 11
4 4 8 12
它很好用!非常感谢您的帮助! – HappyPy
或者到EdChum的解决方案,你可以这样做:
In [163]: pd.DataFrame([[0,0,0]], columns=df.columns).append(df, ignore_index=True)
Out[163]:
a b c
0 0 0 0
1 1 5 9
2 2 6 10
3 3 7 11
4 4 8 12
更具体的数据帧的答案被前置到
对不起你想在加零的行开始,但列应该在这里? 'abc'或'012'? – EdChum
@EdChum:对不起,忘了添加列名。它应该是abc(编辑)。 – HappyPy