根据熊猫列中的列表值从DataFrame中选择行
问题描述:
我是熊猫的新手,我有一个简单的数据框,想基于列提取某些行.但是,此列中的类型是列表.
I am new to pandas and I have a simple dataframe and want to extract certain rows based on a column. However, the type in this column is a list.
示例:
df = pd.DataFrame([['text1', [1,2,3]], ['text2', [2,3,4]]], columns=['text','list_value'])
数据框如下:
text list_value
0 text1 [1, 2, 3]
1 text2 [2, 3, 4]
我尝试了
df.loc[df['list_value'] == [1,2,3]]
它返回一个错误:
ValueError: Arrays were different lengths: 2 vs 3
我想知道是否有比使用for循环迭代数据帧更好的解决方案.
I wonder if there is any better solution than using for loop to iterate the dataframe.
Similar question but the solution is not work for me: Select rows from a DataFrame based on values in a column in pandas.
答
您可以添加apply
tuple
,当单元格中有列表时,熊猫有时会返回有线结果
You can adding apply
tuple
, when there is list in a cell , pandas sometime return the wired result
df.loc[df['list_value'].apply(tuple) == tuple([1,2,3])]
Out[58]:
text list_value
0 text1 [1, 2, 3]