pandas 遍历数据框列 [英] Pandas Iterate over dataframe columns

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

问题描述

对于以字典格式显示给熊猫的数据,如果任何值不在68& amp;的范围内,如何标记数据集(熊猫数据框中的列)? 72?

For my data in dictionary format to pandas, how do I flag datasets (column in my pandas dataframe) if any of the values are outside of the range of 68 & 72?

df = pd.DataFrame(dict(a=[71.5,72.8,79.3],
                       b=[70.2,73.3,74.9],
                       c=[63.1,64.9,65.9],
                       d=[70.1,70.4,70.9]))

我试图做的是创建一个单独的列名称的pandas数据框,如果有任何数据不在68&范围内. 72.有什么提示吗?

What I am attempting to do is create a seperate pandas dataframe of column names if any data is outside of the range of 68 & 72. Any tips?

df_OutOfRange=df[(df.columns<68) | (df.columns>72)]

df_OutOfRange

推荐答案

使用

In [48]: ((df < 68) | (df > 72)).any()
Out[48]:
a     True
b     True
c     True
d    False
dtype: bool

或者,

In [49]: (df.lt(68) | df.gt(72)).any()
Out[49]:
a     True
b     True
c     True
d    False
dtype: bool

或者,

In [62]: df.apply(lambda x: ~x.between(68, 72).all())
Out[62]:
a     True
b     True
c     True
d    False
dtype: bool

详细信息

In [50]: df
Out[50]:
      a     b     c     d
0  71.5  70.2  63.1  70.1
1  72.8  73.3  64.9  70.4
2  79.3  74.9  65.9  70.9

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

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