python dataframe isin,使用多个条件获取新的数据帧pd.Dataframe.isin()

考虑两个数据帧的简单合并,因为默认情况下merge使用所有匹配的名称:from itertools import product

import pandas as pd

def filterby_criteria(df, criteria):

# EXTRACT DICT ITEMS

k,v = criteria.keys(), criteria.values()

# BUILD DF OF ALL POSSIBLE MATCHES

all_matches = (pd.DataFrame(product(*v))

.set_axis(list(k), axis='columns', inplace=False)

)

# RETURN MERGED DF

return df.merge(all_matches)

用随机种子数据演示:

数据

^{pr2}$

函数调用criteria = {"Tool":["python", "r"], "Year":[2013, 2015]}

def filterby_criteria(df, criteria):

k,v = criteria.keys(), criteria.values()

all_matches = (pd.DataFrame(product(*v))

.set_axis(list(k), axis='columns', inplace=False)

)

return df.merge(all_matches)

final_df = filterby_criteria(random_df, criteria)

输出print(final_df)

# Tool Int Num Year

# 0 python 8 96.611384 2015

# 1 python 7 66.782828 2015

# 2 python 9 73.638629 2015

# 3 python 4 70.763264 2015

# 4 python 2 28.311917 2015

# 5 python 3 69.888967 2015

# 6 python 8 97.609694 2015

# 7 python 3 59.198276 2015

# 8 python 3 64.497017 2015

# 9 python 8 87.672138 2015

# 10 python 9 33.605467 2015

# 11 python 8 25.225665 2015

# 12 r 3 72.202364 2013

# 13 r 1 62.192478 2013

# 14 r 7 39.264766 2013

# 15 r 3 14.599786 2013

# 16 r 4 22.963723 2013

# 17 r 1 97.647922 2013

# 18 r 5 60.457344 2013

# 19 r 5 15.711207 2013

# 20 r 7 80.273330 2013

# 21 r 7 74.190107 2013

# 22 r 7 37.923396 2013

# 23 r 2 91.970678 2013

# 24 r 4 31.489810 2013

# 25 r 1 37.580665 2013

# 26 r 2 9.686955 2013

# 27 r 6 56.238919 2013

# 28 r 6 72.820625 2015

# 29 r 3 61.255351 2015

# 30 r 4 45.690621 2015

# 31 r 5 71.143601 2015

# 32 r 6 54.744846 2015

# 33 r 1 68.171978 2015

# 34 r 5 8.521637 2015

# 35 r 7 87.027681 2015

# 36 r 3 93.614377 2015

# 37 r 7 37.918881 2015

# 38 r 3 7.715963 2015

# 39 python 1 42.681928 2013

# 40 python 6 57.354726 2013

# 41 python 1 48.189897 2013

# 42 python 4 12.201131 2013

# 43 python 9 1.078999 2013

# 44 python 9 75.615457 2013

# 45 python 8 12.631277 2013

# 46 python 9 82.227578 2013

# 47 python 7 97.802213 2013

# 48 python 1 57.103964 2013

# 49 python 1 1.941839 2013

# 50 python 3 81.981437 2013

# 51 python 1 56.869551 2013

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值