Python ValueError:无法设置没有定义索引的帧和无法转换为序列的值
我在Python3.X中使用Pandas 0.20.3。我想从另一个pandas数据帧在pandas数据帧中添加一列。两个数据帧都包含51行。所以我使用了以下代码:Python ValueError:无法设置没有定义索引的帧和无法转换为序列的值,python,Python,我在Python3.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.dty
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(组['phone'])
返回pandas.core.series.series
您能建议我需要做哪些更改来删除此错误吗
组['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
你有一列破烂的名单。您唯一的选择是分配列表列表,而不是列表数组(这是
.value
给出的)
在大多数情况下,当返回空数据帧时会出现此错误。对我来说,最好的方法是在使用apply()之前先检查数据帧是否为空
如果len(df)!=0:
df['indicator']=df.apply(分配指示器,轴=1)
您可以使用apply()函数的setresult\u type
参数来“reduce”,而不是使用if语句
问题标题的错误 “ValueError:无法设置没有定义索引的帧和无法转换为序列的值”
如果出于任何原因,该表没有任何行,也可能发生这种情况 解决了我的一天你是天才另一种检查数据帧是否为空的常用方法是:
如果不是df。empty:
对于大多数用户来说,这可能是一个更好的答案,因为你不需要if语句方法所需的条件块和缩进。
class_df['phone'] = group['phone'].tolist()
df['new_column'] = df.apply(func, axis=1, result_type='reduce')