Pandas:根据列内的多个对象值选择行

编程入门 行业动态 更新时间:2024-10-17 07:35:04
本文介绍了Pandas:根据列内的多个对象值选择行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送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:关键词]

本文发布于:2023-04-26 03:45:53,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1128631.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:多个   对象   Pandas

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!