[英]Pandas: Find max value in each row of a column and identify corresponding values in another column
I have a two pandas data frame columns, for which the values are lists of tuples such as: 我有两个pandas数据框列,其值是元组列表,如:
df[‘A’].values
(1.55, 2.07, 2.20, 2.23)
(0.67, 1.10, 1.73, 1.35)
(2.92, 1.98, 2.30, 2.66)
and 和
df[‘B’].values
(1.55, 0.0086, 0.078, 0.12)
(0.672, 0.142, 0.0166, 0.0173)
(1.97, 0.0094, 0.1648, 0.016)
I would like to pick the largest value for each row of df['A']
and find the value in the corresponding position of df['B']
to generate a new column such as: 我想为
df['A']
每一行选择最大值,并在df['B']
的相应位置找到该值以生成新列,例如:
> df[‘C’]
0.12
0.0166
1.97
import pandas as pd
import numpy as np
df_dict = dict(A=[(1.55, 2.07, 2.20, 2.23), (0.67, 1.10, 1.73, 1.35), (2.92, 1.98, 2.30, 2.66)],
B=[(1.55, 0.0086, 0.078, 0.12), (0.672, 0.142, 0.0166, 0.0173), (1.97, 0.0094, 0.1648, 0.016)])
df = pd.DataFrame(df_dict)
Out[180]:
A B
0 (1.55, 2.07, 2.2, 2.23) (1.55, 0.0086, 0.078, 0.12)
1 (0.67, 1.1, 1.73, 1.35) (0.672, 0.142, 0.0166, 0.0173)
2 (2.92, 1.98, 2.3, 2.66) (1.97, 0.0094, 0.1648, 0.016)
def apply_func(row):
return row.B[np.array(row.A).argmax()]
df['C'] = df.apply(apply_func, axis=1)
Out[182]:
A B C
0 (1.55, 2.07, 2.2, 2.23) (1.55, 0.0086, 0.078, 0.12) 0.1200
1 (0.67, 1.1, 1.73, 1.35) (0.672, 0.142, 0.0166, 0.0173) 0.0166
2 (2.92, 1.98, 2.3, 2.66) (1.97, 0.0094, 0.1648, 0.016) 1.9700
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.