你可以使用
numpy
和
reshape
来获得帮助。使用
reshape
可以将每一对数据移动到一个临时的第三维度,然后你就可以有自然的行来包含你想要比较的“有趣”列。
之后,你可以将
reshape
还原为原始数据框的形状。
import pandas as pd
import numpy as np
df = pd.DataFrame(np.random.randint(100, size=(8, 6)), columns = ['A-L', 'A-H', 'B-L', 'B-H', 'C-L', 'C-H'])
m, n = df.shape
data = df.values
data = data.reshape((m, 2, -1), order='F')
cond = (data[:,(0,),:] >= 20) & (data[:,(1,),:] >= 50)
new_data = np.repeat(cond, 2, axis=1)
new_data = new_data.reshape((m, -1), order='F')
new_df = pd.DataFrame(new_data, columns=df.columns)
print(df)
print(new_df)
A-L A-H B-L B-H C-L C-H
0 36 99 0 65 44 41
1 31 52 7 33 27 80
2 39 89 57 18 72 76
3 4 13 11 38 21 9
4 72 4 9 16 78 96
5 26 79 80 53 36 81
6 81 59 93 49 39 24
7 45 49 52 54 63 70
A-L A-H B-L B-H C-L C-H
0 True True False False False False
1 True True False False True True
2 True True False False True True
3 False False False False False False
4 False False False False True True
5 True True True True True True
6 True True False False False False
7 False False True True True True
df [msk]
时,它仅返回在msk中为True的值。 - ABC