你可以使用 df.loc[len(df)] = new_row
在 dataframe 的末尾插入新行。
例如:
import pandas as pd# 建立示例 dataframe
df = pd.DataFrame([[1, 2], [3, 4]], columns=['a', 'b'])
print(df)
# a b
# 0 1 2
# 1 3 4
# 准备新行数据
new_row = [5, 6]
# 在 dataframe 的末尾插入新行
df.loc[len(df)] = new_row
print(df)
# a b
# 0 1 2
# 1 3 4
# 2 5 6
注意:如果你想在 dataframe 的任意位置插入新行,你可以使用类似 df.loc[row_index] = new_row
的语法。