按值过滤系列中的字典

时间:2021-02-10 04:27:27

I have a dataframe like this:

我有一个这样的数据帧:

In [23]: df
Out[23]: 
                             scope
0  {'range': 2, 'category': '234'}
1  {'range': 1, 'category': '222'}

I would like to filter rows by condition "range==1".How can I do it?

我想按条件“range == 1”过滤行。我该怎么做?

2 个解决方案

#1


0  

One way is to use pd.Series.apply to create a Boolean indexer.

一种方法是使用pd.Series.apply来创建布尔索引器。

import pandas as pd

df = pd.DataFrame({'scope': [{'range': 2, 'category': '234'},
                             {'range': 1, 'category': '222'}]})

res = df[df['scope'].apply(lambda x: x['range'] == 1)]

#                              scope
# 1  {'range': 1, 'category': '222'}

#2


0  

Here is another way

这是另一种方式

import pandas as pd
filter = pd.DataFrame({"scope": [i for i in df["scope"] if i["range"] == 1]})

#1


0  

One way is to use pd.Series.apply to create a Boolean indexer.

一种方法是使用pd.Series.apply来创建布尔索引器。

import pandas as pd

df = pd.DataFrame({'scope': [{'range': 2, 'category': '234'},
                             {'range': 1, 'category': '222'}]})

res = df[df['scope'].apply(lambda x: x['range'] == 1)]

#                              scope
# 1  {'range': 1, 'category': '222'}

#2


0  

Here is another way

这是另一种方式

import pandas as pd
filter = pd.DataFrame({"scope": [i for i in df["scope"] if i["range"] == 1]})