我已经看过这个了,我认为我应该拥有的东西,但事实并非如此。正在评估第一个条件(> = 80),但没有评估第二个条件(<= 100)。
我希望ANY列值在80和100之间(包括两端)的每一行,但如果任何列值大于100,则将其排除。
我应该只看到AP-2,AP-8和AP-9行。
import pandas as pd
df = pd.DataFrame({'AP-1': [30, 32, 34, 31, 33, 35, 36, 38, 37],
'AP-2': [30, 32, 34, 80, 33, 35, 36, 38, 37],
'AP-3': [30, 32, 81, 31, 33, 101, 36, 38, 37],
'AP-4': [30, 32, 34, 95, 33, 35, 103, 38, 121],
'AP-5': [30, 32, 34, 31, 33, 144, 36, 38, 37],
'AP-6': [30, 32, 34, 31, 33, 35, 36, 110, 37],
'AP-7': [30, 87, 34, 31, 111, 35, 36, 38, 122],
'AP-8': [30, 32, 99, 31, 33, 35, 36, 38, 37],
'AP-9': [30, 32, 34, 31, 33, 99, 88, 38, 37]}, index=['1', '2', '3', '4', '5', '6', '7', '8', '9'])
df1 = df.transpose()
print(df1)
print()
df2 = df1[(df1.values >= 80).any(1) & (df1.values <= 100).any(1)]
print(df2)
df2显示为:
1 2 3 4 5 6 7 8 9
AP-2 30 32 34 80 33 35 36 38 37
AP-3 30 32 81 31 33 101 36 38 37
AP-4 30 32 34 95 33 35 103 38 121
AP-5 30 32 34 31 33 144 36 38 37
AP-6 30 32 34 31 33 35 36 110 37
AP-7 30 87 34 31 111 35 36 38 122
AP-8 30 32 99 31 33 35 36 38 37
AP-9 30 32 34 31 33 99 88 38 37
答案 0 :(得分:1)
啊,我明白了。我需要.all(1)作为<= 100。
答案 1 :(得分:1)
这是另一个主意,请分开遮罩并使用&进行连接:
import pandas as pd
df = pd.DataFrame({'AP-1': [30, 32, 34, 31, 33, 35, 36, 38, 37],
'AP-2': [30, 32, 34, 80, 33, 35, 36, 38, 37],
'AP-3': [30, 32, 81, 31, 33, 101, 36, 38, 37],
'AP-4': [30, 32, 34, 95, 33, 35, 103, 38, 121],
'AP-5': [30, 32, 34, 31, 33, 144, 36, 38, 37],
'AP-6': [30, 32, 34, 31, 33, 35, 36, 110, 37],
'AP-7': [30, 87, 34, 31, 111, 35, 36, 38, 122],
'AP-8': [30, 32, 99, 31, 33, 35, 36, 38, 37],
'AP-9': [30, 32, 34, 31, 33, 99, 88, 38, 37]},
index=['1', '2', '3', '4', '5', '6', '7', '8', '9'])
# This is the actual frame you want
df = df.transpose()
m1 = (df >= 80).any(1)
m2 = ~(df >= 100).any(1) #<-- Invert the statement with ~
df2 = df.loc[m1&m2]
print(df2)
打印:
1 2 3 4 5 6 7 8 9
AP-2 30 32 34 80 33 35 36 38 37
AP-8 30 32 99 31 33 35 36 38 37
AP-9 30 32 34 31 33 99 88 38 37