当dataframe元素的值是列表时如何使用.loc [python pandas] [英] How to use .loc when the value of dataframe elements are list [python pandas]

查看:520
本文介绍了当dataframe元素的值是列表时如何使用.loc [python pandas]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据框df,其中df.trajec中的元素是一个列表.

I have a dataframe df where the elements in df.trajec is a list.

例如df.ix['smith']['trajec'] = ['a', 'b', 'c', 'a', 'b']

type(df.ix['smith']) = list

在这种情况下,我发现我无法使用该命令.

In this case, I found out that I cannot use such command.

aaa = ['a', 'b', 'c', 'a', 'b']
df.loc[df.trajec == aaa]

它给了我以下错误消息.

And it gives me an error message like below.

ValueError: Arrays were different lengths: 8886 vs 5

有什么方法可以找到数据帧df的子集,其中df.trajec等于列表aaa吗?

Is there any way to find the subset of the dataframe df where df.trajec is equal to a list aaa?

推荐答案

您需要 apply 用于创建mask:

df = pd.DataFrame({'trajec':[['a', 'b', 'c', 'a', 'b'],
                             ['a', 'b'],
                             ['a','c', 'b']]}, 
                   index=['smith','smith1','smith2'])

print (df)
                 trajec
smith   [a, b, c, a, b]
smith1           [a, b]
smith2        [a, c, b]

aaa = ['a', 'b', 'c', 'a', 'b']
mask = df.trajec.apply(lambda x: x == aaa)
print (mask)
smith      True
smith1    False
smith2    False
Name: trajec, dtype: bool

#loc can be omit if need filter all columns
print (df[mask])
                trajec
smith  [a, b, c, a, b]

#if need apply mask and return only column `trajec`
print (df.loc[mask, 'trajec'])
smith    [a, b, c, a, b]
Name: trajec, dtype: object

另一个可能的masklist comprehension:

mask = [x == aaa for x in df.trajec.values]
print (mask)
[True, False, False]

print (df[mask])
                trajec
smith  [a, b, c, a, b]

这篇关于当dataframe元素的值是列表时如何使用.loc [python pandas]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆