我在我的 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/