向dataframe中添加行

向 dataframe 中添加行建议使用 loc

First Step:事先准备一个与原数据列的个数相同的list或者是Series,比如[1, 2, 3, 4]

# 先定义一个 DataFream
import pandas as pd
df = pd.DataFrame([[1, 2, 3, 4], [5, 6, 7, 8]])

Initial DataFrame:

   0  1  2  3
0  1  2  3  4
1  5  6  7  8

1. iloc[]

用iloc向 dataframe 中添加行使用如下代码会报错IndexError: iloc cannot enlarge its target object

# 向第[2]行添加
df.iloc[2] = [9, 10, 11, 12]

原因是dataframe没有第[2]行, 而 iloc 只能在原先有数据的行进行修改,不能插入新的行。

2.loc[]

使用loc向 dataframe 添加行

df.loc[2] = [9, 10, 11, 12]
print(df)

# output

0	1	2	3
0	1	2	3	4
1	5	6	7	8
2	9	10	11	12

# 成功插入了

# 输出df的行index
df.index
Int64Index([0, 1, 2], dtype='int64')

在loc中向 dataframe 添加行的原理是,loc[2] 去找 index 为 2 的行,如果没有则创建一个index为2的行,然后再执行 df.loc[2] = [9, 10, 11, 12]

posted @ 2022-12-07 16:53  小夏虫  阅读(2124)  评论(3编辑  收藏  举报