在数据帧中进行条件处理时使用Pythonic方式具有多个Or的方法 [英] Pythonic Way to have multiple Or's when conditioning in a dataframe

查看:58
本文介绍了在数据帧中进行条件处理时使用Pythonic方式具有多个Or的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上代替了写作

data[data['pos'] == "QB" | data['pos'] == "DST" | ...]

我想检查很多情况的地方

where there are many cases I want to check

我正在尝试做与此类似的事情什么是pythonic方法做多次题?.但是,这

I was trying to do something similar to this What's the pythonic method of doing multiple ors?. However, this

data[data['pos'] in ("QB", "DST", ...)]

不起作用.

我在此处阅读了文档 http://pandas.pydata.org/pandas-docs/stable/gotchas.html ,但我仍然遇到问题.

I read the documentation here http://pandas.pydata.org/pandas-docs/stable/gotchas.html but I'm still having issues.

推荐答案

您正在寻找的是

What you are looking for is Series.isin . Example -

data[data['pos'].isin(("QB", "DST", ...))]

这将检查pos中的每个值是否在值列表-("QB", "DST", ...)中.类似于您的多个|会执行的操作.

This would check if each value from pos is in the list of values - ("QB", "DST", ...) . Similar to what your multiple | would do.

这篇关于在数据帧中进行条件处理时使用Pythonic方式具有多个Or的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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