根据pandas数据框中的多个列值选择行 [英] selecting rows based on multiple column values in pandas dataframe

查看:90
本文介绍了根据pandas数据框中的多个列值选择行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个pandas DataFrame df:

import pandas as pd

data = {"Name": ["AAAA", "BBBB"],
        "C1": [25, 12],
        "C2": [2, 1],
        "C3": [1, 10]}

df = pd.DataFrame(data)
df.set_index("Name")

打印时看起来像这样(供参考):

which looks like this when printed (for reference):

      C1  C2  C3
Name            
AAAA  25   2   1
BBBB  12   1  10

我想选择C1C2C3的值在020之间的行.

I would like to choose rows for which C1, C2 and C3 have values between 0 and 20.

您能建议一种优雅的方式来选择那些行吗?

Can you suggest an elegant way to select those rows?

推荐答案

我认为下面应该这样做,但其优雅之处尚待争论.

I think below should do it, but its elegance is up for debate.

new_df = old_df[((old_df['C1'] > 0) & (old_df['C1'] < 20)) & ((old_df['C2'] > 0) & (old_df['C2'] < 20)) & ((old_df['C3'] > 0) & (old_df['C3'] < 20))]

这篇关于根据pandas数据框中的多个列值选择行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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