如何将 pandas 列的值设置为列表 [英] How to set the value of a pandas column as list
问题描述
我想将pandas列的值设置为字符串列表.但是,我这样做的努力没有成功,因为熊猫将列值视为可迭代的,并且得到了:ValueError: Must have equal len keys and value when setting with an iterable
.
I want to set the value of a pandas column as a list of strings. However, my efforts to do so didn't succeed because pandas take the column value as an iterable and I get a: ValueError: Must have equal len keys and value when setting with an iterable
.
这是MWE
>> df = pd.DataFrame({'col1': [1, 2, 3], 'col2': [4, 5, 6]})
>> df
col1 col2
0 1 4
1 2 5
2 3 6
>> df['new_col'] = None
>> df.loc[df.col1 == 1, 'new_col'] = ['a', 'b']
ValueError: Must have equal len keys and value when setting with an iterable
我试图使用df.new_col = df.new_col.astype(list)
将dtype
设置为list
,但这也不起作用.
I tried to set the dtype
as list
using df.new_col = df.new_col.astype(list)
and that didn't work either.
我想知道什么是正确的方法.
I am wondering what would be the correct approach here.
编辑
此处提供的答案:使用at
的Python熊猫将列表插入单元格对我也不起作用.
The answer provided here: Python pandas insert list into a cell using at
didn't work for me either.
推荐答案
不容易,一种可行的解决方案是创建助手Series
:
Not easy, one possible solution is create helper Series
:
df.loc[df.col1 == 1, 'new_col'] = pd.Series([['a', 'b']] * len(df))
print (df)
col1 col2 new_col
0 1 4 [a, b]
1 2 5 NaN
2 3 6 NaN
另一种解决方案,如果也需要将缺失值设置为空列表,则使用列表理解:
Another solution, if need set missing values to empty list too is use list comprehension:
#df['new_col'] = [['a', 'b'] if x == 1 else np.nan for x in df['col1']]
df['new_col'] = [['a', 'b'] if x == 1 else [] for x in df['col1']]
print (df)
col1 col2 new_col
0 1 4 [a, b]
1 2 5 []
2 3 6 []
但是随后,您将失去使用连续内存块中保存的NumPy数组的矢量化功能.
But then you lose the vectorised functionality which goes with using NumPy arrays held in contiguous memory blocks.
这篇关于如何将 pandas 列的值设置为列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!