问题描述
限时送ChatGPT账号..我有一个 Pandas 数据框,其中一列包含用户信息.此列的每条记录都是一个列表,该列表又包含用户信息的字典.像下面这样:
I have a pandas dataframe in which one of the column contains user information. Each record of this column is a list which in turn contains dictionaries of user information. Like the follwoing:
USER id
1 [{u'STATUS': u'INACTV', u'NAME': 'abc'},{u'STATUS': u'ACTV', u'NAME': 'xyz'}] 634618
2 [{u'STATUS': u'INACTV', u'NAME': 'abc'},{u'STATUS': u'ACTV', u'NAME': 'xyz'}] 642054
3 [{u'STATUS': u'ACTV', u'NAME': 'abc'},{u'STATUS': u'ACTV', u'NAME': 'xyz'}] 631426
我只想选择 STATUS 为 ACTV 且 NAME 为 abc 的行.如何选择嵌套数据的行.所以在上面的df中只会选择第3行
I want to select only the rows where the STATUS is ACTV and the NAME is abc. How do I select rows where the data is nested. So in the above df only row 3 will be selected
推荐答案
我们可以将您的 df.USER
列解压到 pd.Panel
中,然后以这种方式找到行.很多开销.不值得!但是很酷……也许吧.我稍后再试.
We can unpack your df.USER
column into a pd.Panel
and find the rows that way. Lots of overhead. Not worth it! But cool... maybe. I'll try again later.
pn = pd.Panel({k: pd.DataFrame(v) for k, v in df.USER.iteritems()})
cond1 = pn.loc[:, :, 'STATUS'] == 'ACTV'
cond2 = pn.loc[:, :, 'NAME'] == 'abc'
df.loc[pn.loc[(cond1 & cond2).any(), :, :].items]
USER id
2 [{'STATUS': 'ACTV', 'NAME': 'abc'}, {'STATUS':... 631426
这篇关于Pandas:根据列内的多个对象值选择行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
更多推荐
[db:关键词]
发布评论