gpt4 book ai didi

python - 查找行值最小的 pandas DataFrame 的列名

转载 作者:行者123 更新时间:2023-12-05 04:01:50 25 4
gpt4 key购买 nike

我有一个 DataFrame 如下:

X = np.array([[1.0, -20, 200, 50],
[2.0, 19, 100, 52],
[3.0, 17, -150, 55],
[4.0, 20, -120, 60],
[5.0, 21, 119, 70],
[6.0, -15, 134, -75],
[7.0, 9, 178, -80],
[8.0, 10, -190, 90],
[9.0, 19, 200, 70],
[10.0, 20, 210, 65]])

native_id1 = ['08MB005', '08ME005', '08GD006','08GH002']
native_id2 = ['08CD001', '08EF006', '08TH002', '08LO002', '07HP003', '08IK002', '09WF001','09YU001', '05KJ008', '08LK007']
X = pd.DataFrame(X, native_id2, native_id1)

我使用了下面的函数,该函数返回与每行中的最小值相对应的列或索引的位置。

idx = np.argmin(X.values, axis=1)

返回类似的东西:

array([1, 0, 2, 2, 0, 3, 3, 2, 0, 0], dtype=int64)

我要找的是这个:

08CD001    08ME005
08EF006 08MB005
08TH002 08GD006
08LO002 08GD006
07HP003 08MB005
08IK002 08GH002
09WF001 08GH002
09YU001 08GD006
05KJ008 08MB005
08LK007 08MB005

* 已更新 *我找到了这个问题的答案,我试图找到与 Pandas 数据框每一行的最小值相对应的列名。

下面的答案有效:

df.idxmin(axis=1)

最佳答案

使用 DataFrame.idxmin ( docs ):

df.idxmin(axis=1)

等效地,您可以在 df.apply(np.argmin, axis=1) 中使用 np.argmin。但是你会收到这个警告:

FutureWarning: 'argmin' is deprecated. Use 'idxmin' instead. The behavior of 'argmin' will be corrected to return the positional minimum in the future. Use 'series.values.argmin' to get the position of the minimum now.

所以推荐idxmin

关于python - 查找行值最小的 pandas DataFrame 的列名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54931167/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com