pandas 按两列过滤(python) [英] pandas filter by two columns (python)

查看:114
本文介绍了 pandas 按两列过滤(python)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有许多列的pandas DataFrame(df),其中两个是"Year"和"col_1"

I have a pandas DataFrame (df) with many columns, two of which are "Year" and "col_1"

我在列表(条件)中也总结了一个提取条件:

I also have a extraction criteria summarised in a list(Criteria):

[1234,5432,...,54353,654,1234].

[1234,5432,...,54353,654,1234].

如果要满足以下条件,我想提取此DataFrame的子集:

I would like to extract the subset of this DataFrame if the following criteria are met:

((df.Year==1990) & (df.col_1>=Criteria[0])) or

((df.Year==1991) & (df.col_1>=Criteria[1])) or

((df.Year==1992) & (df.col_1>=Criteria[2])) or 

...

((df.Year==2010) & (df.col_1>=Criteria[20])) or

((df.Year==2011) & (df.col_1>=Criteria[21]))

尽管我可以列出所有这些条件的组合,但我想 只需短短一行即可完成此操作,例如:

Although I can list out all the combination of these criteria, I would like to do this in one short line, something like:

df = df[df[['col_1','col_2']].apply(lambda x: f(*x), axis=1)]

(来自如何过滤熊猫多列的数据框)

请告知我该怎么做.谢谢.

Please advise how I can do it. Thank you.

推荐答案

Sample DataFrame:

df = pd.DataFrame({'col_1':[2000,1,54353,5],
                   'Year':[1990,1991,1992,1993],
                   'a':range(4)})

print (df)
   col_1  Year  a
0   2000  1990  0
1      1  1991  1
2  54353  1992  2
3      5  1993  3

按条件和年份组合创建助手dictionary:

Create helper dictionary by criteria and years combinations:

Criteria = [1234,5432,54353,654,1234]
years = np.arange(1990, 1990 + len(Criteria))
d = dict(zip(years, Criteria))
print (d)
{1990: 1234, 1991: 5432, 1992: 54353, 1993: 654, 1994: 1234}

最后 map year并按 boolean indexing 进行过滤:

Last map by column year and filter by boolean indexing:

df = df[df['col_1'] >= df['Year'].map(d)]
print (df)
   col_1  Year  a
0   2000  1990  0
2  54353  1992  2

详细信息:

print (df['Year'].map(d))
0     1234
1     5432
2    54353
3      654
Name: Year, dtype: int64

print (df['col_1'] >= df['Year'].map(d))

0     True
1    False
2     True
3    False
dtype: bool

这篇关于 pandas 按两列过滤(python)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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