我想创建一个新的df列来保存列表值。
当我仅仅将一个包含列表值的列进行赋值测试时,我得到了类似的错误。
def add_list_values(row): # row parameter is needed but not in this sample code
list_val = [1, 'OKOKOK', 2123]
return list_val
df['new_col'] = df.apply(add_list_values, axis=1)
Error: ValueError: Shape of passed values is (91, 4), indices imply (91, 2)
当我仅仅将一个包含列表值的列进行赋值测试时,我得到了类似的错误。
df['new_col2'] = [1, 'OKOKOK', 2123]
ValueError: Length of values does not match length of index
[1, 'OKOKOK', 2123]
has a length of 3, in your example the length ofdf
appears to be longer than that. To assign the list[1, 'OKOKOK', 2123]
to every row in thedf
do something likedf['new_col2'] = [[1, 'OKOKOK', 2123] for x in df.index]
- jtweeder