python - Pandas dataframe,连续查找所选列中的最大值,并根据该值查找另一列的值

标签 python pandas

我有一个这样的数据框:

>>>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/

相关文章:

python - Python3 的 pathlib 可以在 Linux 和 Windows 系统之间移植使用吗?

python - While 循环计数器困惑

python - Pandas,根据其他列值删除重复行

python - Pandas - 计算每个系列中的元素数量

python - 如何在 Python(或 R)中根据特定条件进行聚合和求和

python - 如何将值插入到新创建的空数据框的列中?

python - Pandas 数据框查询字符串参数中的单引号

python - 使用 Python 如何从 Excel 文件获取输入、定义函数并在该 Excel 文件的新工作表中生成输出?

python - pd.pivot_table 当列是日期时

python - 图像去噪分割图