简体   繁体   English

熊猫遍历数据框列

[英]Pandas Iterate over dataframe columns

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? 对于字典格式的数据到熊猫,如果任何值超出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]))

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? 我要尝试做的是,如果任何数据不在68和72的范围内,则创建一个单独的列名称的pandas数据框。

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

df_OutOfRange

Use 采用

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

Or, 要么,

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

Or, 要么,

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

Details 细节

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM