我有两个数据框,将一年中的不同季度作为列,将特定位置作为行:
温度:
q_1 | q_2 | q_3 | q_4 | |
---|---|---|---|---|
A | 10 | 50 | 0 | 5 |
B | 6 | 0 | 30 | 1 |
C | 60 | 2 | 9 | 16 |
降水
q_1 | q_2 | q_3 | q_4 | |
---|---|---|---|---|
A | 18 | 1 | 0 | 7 |
B | 6 | 13 | 12 | 3 |
C | 3 | 20 | 4 | 0 |
我正在尝试创建另一个数据框,其列由每个位置最潮湿/最干燥季度的温度和最温暖/最凉爽季度的降水填充:
DF_new:
temp_wettest | temp_dryest | precip_warmest | precip_coolest | |
---|---|---|---|---|
A | 10 | 0 | 1 | 0 |
B | 0 | 1 | 12 | 13 |
C | 2 | 16 | 3 | 20 |
我一直在尝试使用 idxmax:
temp = pd.DataFrame({'q_1' : [10,6,60],
'q_2' : [50,0,2],
'q_3' : [0,30,9],
'q_4' : [5,1,16]},index=['A','B','C'])
prec = pd.DataFrame({'q_1' : [18,6,3],
'q_2' : [1,13,20],
'q_3' : [0,12,4],
'q_4' : [7,3,0]},index=['A','B','C'])
DF_new = pd.DataFrame({'temp_wettest': temp[prec.idxmax(axis=1)],
'temp_driest' : temp[prec.idxmin(axis=1)],
'precip_warmest': prec[temp.idxmax(axis=1)],
'precip_coolest': prec[temp.idxmin(axis=1)]},index=['A','B','C'])
<OUT>
temp_wettest temp_driest precip_warmest precip_coolest
A (q, _, 1) (q, _, 3) (q, _, 2) (q, _, 3)
B (q, _, 2) (q, _, 4) (q, _, 3) (q, _, 2)
C (q, _, 2) (q, _, 4) (q, _, 1) (q, _, 2)
我知道为什么 idxmax 不起作用(它只是传入一个列名列表),但我不确定如何将实际值放入新数据框中。我也试过使用 pd.apply(),但我不确定使用什么函数。
谢谢
答案 0 :(得分:1)
如果您的 Pandas 版本 <1.2.0,请尝试 lookup
:
DF_new = pd.DataFrame({'temp_wettest': temp.lookup(prec.index, prec.idxmax(axis=1)),
'temp_driest' : temp.lookup(prec.index, prec.idxmin(axis=1)),
'precip_warmest': prec.lookup(temp.index, temp.idxmax(1)),
'precip_coolest': prec.lookup(temp.index, temp.idxmin(1))
})
输出:
temp_wettest temp_driest precip_warmest precip_coolest
0 10 0 1 0
1 0 1 12 13
2 2 16 3 20
答案 1 :(得分:1)
我不知道 lookup
的最佳替代方案,但这可能有效。
DF_new = pd.DataFrame({'temp_wettest': temp.stack().loc[list(map(tuple,prec.idxmax(axis=1).reset_index().to_numpy()))].tolist(),
'temp_driest' : temp.stack().loc[list(map(tuple,prec.idxmin(axis=1).reset_index().to_numpy()))].tolist(),
'precip_warmest': prec.stack().loc[list(map(tuple,temp.idxmax(axis=1).reset_index().to_numpy()))].tolist(),
'precip_coolest': prec.stack().loc[list(map(tuple,temp.idxmin(axis=1).reset_index().to_numpy()))].tolist()})