我有一个这样的数据框:
>>>import pandas as pd
>>>df = pd.DataFrame({'x1':[20,25],'y1':[5,8],'x2':[22,27],'y2':[10,2]})
>>>df
x1 y1 x2 y2
0 20 5 22 10
1 25 8 27 2
>>>
X 和 Y 配对在一起。我需要比较 y1 和 y2 并在每一行中获得最大值。并找到对应的x。 因此第[0]行的最大值是y2(=10),对应的x是x2(=22)。第二行将是 y1 (=8) 和 x1(=25)。 预期结果,新列 x 和 y:
x1 y1 x2 y2 x y
0 20 5 22 10 22 10
1 25 8 27 2 25 8
这是我为详细说明问题而制作的一个简单数据框。 X 和 Y 对,在我的例子中,可以是 30 对。
最佳答案
# get a hold on "y*" columns
y_cols = df.filter(like="y")
# get the maximal y-values' suffixes, and then add from front "x" to them
max_x_vals = y_cols.idxmax(axis=1).str.extract(r"(\d+)$", expand=False).radd("x")
# get the locations of those x* values
max_x_ids = df.columns.get_indexer(max_x_vals)
# now we have the indexes of x*'s in the columns; NumPy's indexing
# helps to get a cross section
df["max_xs"] = df.to_numpy()[np.arange(len(df)), max_x_ids]
# for y*'s, it's directly the maximum per row
df["max_ys"] = y_cols.max(axis=1)
得到
>>> df
x1 y1 x2 y2 max_xs max_ys
0 20 5 22 10 22 10
1 25 8 27 2 25 8
关于python - Pandas dataframe,连续查找所选列中的最大值,并根据该值查找另一列的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74918325/