pandas :比较系列中的列表对象 [英] Pandas: compare list objects in Series
本文介绍了 pandas :比较系列中的列表对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在我的数据框中,一列由列表组成,例如:
In my dataframe a column is made up of lists, for example:
df = pd.DataFrame({'A':[[1,2],[2,4],[3,1]]})
我需要找出列表[1,2]在此数据框中的位置.我试过了:
I need to find out the location of list [1,2] in this dataframe. I tried:
df.loc[df['A'] == [1,2]]
和
df.loc[df['A'] == [[1,2]]]
但是完全失败了.比较似乎很简单,但这是行不通的.我在这里想念东西吗?
but failed totally. The comparison seems very simple but that just doesn't work. Am I missing something here?
推荐答案
请勿在单元格中使用list
,这会给pandas
带来很多问题.如果确实需要object
列,请使用tuple
:
Do not use list
in cell, it creates a lot of problem for pandas
. If you do need an object
column, using tuple
:
df.A.map(tuple).isin([(1,2)])
Out[293]:
0 True
1 False
2 False
Name: A, dtype: bool
#df[df.A.map(tuple).isin([(1,2)])]
这篇关于 pandas :比较系列中的列表对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文