dict存到pandas_根据存储在dict中的条件从Pandas数据帧中选择数据

I have a Pandas dataframe that contains a large number of variables. This can be simplified as:

tempDF = pd.DataFrame({ 'var1': [12,12,12,12,45,45,45,51,51,51],

'var2': ['a','a','b','b','b','b','b','c','c','d'],

'var3': ['e','f','f','f','f','g','g','g','g','g'],

'var4': [1,2,3,3,4,5,6,6,6,7]})

If I wanted to select a subset of the dataframe (e.g. var2='b' and var4=3), I would use:

tempDF.loc[(tempDF['var2']=='b') & (tempDF['var4']==3),:]

However, is it possible to select a subset of the dataframe if the matching criteria are stored within a dict, such as:

tempDict = {'var2': 'b','var4': 3}

It's important that the variable names are not predefined and the number of variables included in the dict is changeable.

I've been puzzling over this for a while and so any suggestions would be greatly appreciated.

解决方案

You could create mask for each condition using list comprehension and then join them by converting to dataframe and using all:

In [23]: pd.DataFrame([tempDF[key] == val for key, val in tempDict.items()]).T.all(axis=1)

Out[23]:

0 False

1 False

2 True

3 True

4 False

5 False

6 False

7 False

8 False

9 False

dtype: bool

Then you could slice your dataframe with that mask:

mask = pd.DataFrame([tempDF[key] == val for key, val in tempDict.items()]).T.all(axis=1)

In [25]: tempDF[mask]

Out[25]:

var1 var2 var3 var4

2 12 b f 3

3 12 b f 3

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值