我有四列的值:“A”、“B”、“C”和“D”,还有一列包含这四个字母中的任意一个。
Letter A B C D max
12063289 D 5 9 0 39 0
12063290 D 3 25 0 79 0
12063291 B 0 232 0 0 0
12063292 A 351 0 0 0 0
12063293 D 0 0 0 394 0
根据字母列的值,我想计算其他三列的最大值。
Letter A B C D max
12063289 D 5 9 0 39 9
12063290 D 3 25 0 79 25
12063291 B 0 232 0 16 16
12063292 A 351 0 200 0 200
12063293 D 0 0 0 394 0
假设上述数据存储在变量 df 中,我尝试执行以下操作:
import numpy as np
import pandas as pd
columns = {'A':['B','C','D'],
'B':['A','C','D'],
'C':['A','B','D'],
'D':['A','B','C']}
for letter in ['A', 'B', 'C', 'D']:
mask = df.loc[df['Letter']==letter]
np.max(df[mask][columns[letter]], out=df[mask]['max'])
我基本上希望 max 函数仅在屏蔽数据帧的相关列上工作,并在完全正确的位置写回原始数据帧 (df) 的“最大”列,但 pandas 在最后一行提示:
ValueError: Must pass DataFrame with boolean values only
问题是我如何准确定位这些行的那些单元格以接收 max() 函数的输出,以便不使用不必要的空间(我可以使用 apply 函数来做到这一点,但它占用了大量空间我没有)。
请您参考如下方法:
申请
cols = list('ABCD')
df.apply(lambda x: x.loc[cols].drop(x.Letter).max(), 1)
12063289 9
12063290 25
12063291 16
12063292 200
12063293 0
dtype: int64
set_index + query + groupby
d1 = df.set_index(
'Letter', append=True
)[list('ABCD')].rename_axis('Col', 1).stack().to_frame('value')
d1.query('Letter != Col').groupby(level=0).value.max()
12063289 9
12063290 25
12063291 16
12063292 200
12063293 0
dtype: int64
