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