在Pandas数据框中添加带索引的空行

nbewdwxp  于 2023-03-28  发布在  其他
关注(0)|答案(2)|浏览(123)

在我看到的所有示例和答案中,如果需要在Pandas数据框中添加空行,则都用途:

ignore_index=True

如果我想离开当前索引,并向具有给定索引的 Dataframe 追加一个空行,该怎么办?

toe95027

toe950271#

我们使用reindex

df.reindex(df.index.values.tolist()+['Yourindex'])
Out[1479]: 
             A    B
0          one   Aa
1          one   Bb
2          two   Cc
Yourindex  NaN  NaN

数据输入

df = pd.DataFrame({'A' : ['one', 'one', 'two'] ,
                   'B' : ['Aa', 'Bb', 'Cc'] })
l5tcr1uw

l5tcr1uw2#

我用这种方法…

df = pd.DataFrame({'A' : ['one', 'one', 'two'] ,
                   'B' : ['Aa', 'Bb', 'Cc'] })
new_df = pd.concat([df, pd.DataFrame(index=pd.Index(['New Index']))])

相关问题