python - 值错误 : Cannot set a frame with no defined index and a value that cannot be converted to a Series

标签 python

我在我的 python 3.X 中使用 Pandas 0.20.3。我想在另一个 Pandas 数据框中的 Pandas 数据框中添加一列。两个数据框都包含 51 行。所以我使用了以下代码:

class_df['phone']=group['phone'].values

我收到以下错误消息:

ValueError: Cannot set a frame with no defined index and a value that cannot be converted to a Series

class_df.dtypes 给我:

Group_ID         object
YEAR             object
Terget           object
phone            object
age              object

type(group['phone']) 返回 pandas.core.series.Series

您能否建议我需要做哪些更改才能消除此错误?

group['phone']的前5行如下:

0    [735015372, 72151508105, 7217511580, 721150431...
1                                                   []
2    [735152771, 7351515043, 7115380870, 7115427...
3    [7111332015, 73140214, 737443075, 7110815115...
4    [718218718, 718221342, 73551401, 71811507...
Name: phoen, dtype: object

最佳答案

在大多数情况下,当您返回空数据框时会出现此错误。对我有用的最佳方法是在使用 apply() 之前先检查数据框是否为空

if len(df) !=  0:
    df['indicator'] = df.apply(assign_indicator, axis=1)

关于python - 值错误 : Cannot set a frame with no defined index and a value that cannot be converted to a Series,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48306694/

相关文章:

python - 在python中实现sigmoid函数

python - Pytest:集合错误,函数不使用参数 'date'

python - Holoviews 示例 : How to display plot in Jupyter notebook?

Python - 如何使用管道执行 shell 命令,但没有 'shell=True' ?

python - Jupyter - 从导入的笔记本导入类

python - 在 Django-rest-framework 中编写一个可以访问 validatated_data 的验证器类

Python - 来自 2 个文件的行与\n

python - 如何删除Python中特定字符之前的所有字符?

python - 允许我的 python 脚本的 Makefile?

python - 当我捕捉到异常时,如何获取上一帧的类型、文件和行号?