python - 在 pandas 上添加新行?

标签 python pandas

为什么我无法在 pandas 上添加新行?

data="""lname    fname   rno_cd    eri_cd
0    CRUISE   TOM     E         1
1    DEPP     JOHNNY  Y         0
2    DICAPR   LENARDO Nan       1
3    PITT     BRAD    Nan       1
4    MOST     JEFF    A         0
5    HANKS    TOM     Nan       1
6    BRANDO   MARLON  C         1
7    WILLIAMS ROBIN   F         1
8    DOWNEY   ROBERT  B         1
9    PACINO   AL      E         1"""

import pandas as pd
from io import StringIO

df= pd.read_csv(StringIO(data.decode('UTF-8')),delim_whitespace=True )


df.loc[-1]= ['2','3','4','4']

每当我尝试执行代码时都会收到错误。

我也尝试过:

df.loc[len(df)+1]

最佳答案

尝试 .iloc 属性:

df.iloc[-1] = []

更多 here .

关于python - 在 pandas 上添加新行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27466290/

相关文章:

python - 将 Python 函数应用于 Pandas 分组数据帧 - 加速计算的最有效方法是什么?

python - 从两个 DataFrame 列构建字典

python - 我可以将切片器存储在变量中吗? ( Pandas / python )

python - Python3解释器中方法和类的位置

python - 我如何在 tkinter 时使用 Work

python - Pandas 按列值拆分 DataFrame

python - 简单数据集 regplot 中的 keyerror - AttributeError : 'str' object has no attribute 'conjugate'

python - 安装了 pip,但找不到 virtualenvwrapper_bashrc 在哪里

python - 如何将xls转换为xlsx

python - 使用 django_nose 和 django-dynamic-fixture 优化示例 django 模型的设置和拆卸