pandas - 从 idxmax 获取系列
问题描述
假设我有一个这样的数据框:
A B C
0 123 3 2
1 2 8 1
2 4 11 3
3 rows x 3 columns
我有它各自的idxmax()
系列
A 0
B 2
3 2
Lenght: 3, dtype: int64
现在,我有第二个数据框:
A B C
0 'a' 7 'c'
1 'b' 5 'a'
2 'c' 2 'd'
因此,使用idxmax()
之前计算的系列,我想检索每列中的相应值,并获取系列
A 'a'
B 5
C 'd'
Lenght: 3, dtype: object
解决方案
DataFrame.lookup
与构造函数一起使用Series
:
s = df1.idxmax()
a = pd.Series(df2.lookup(s, s.index), index=s.index)
print (a)
A 'a'
B '2'
C 'd'
dtype: object
推荐阅读
- ios - 处理 Swift 中由 3rd 方库编写的目标 c 引起的异常
- flutter - 我如何使用 ReorderableListView,特别是我在“onReorder”中放了什么
- javascript - 通过 WebPack 在多个文件中使用 Vuex 存储
- docker - 来自 hub.docker.com 的 Docker 映像统计信息
- javascript - 直接从浏览器中的输入将资产上传到内容(SDK CMA)时出错
- codenameone - SideMenu - 智能手机和带有智能手机皮肤的模拟器上的宽度错误
- marklogic - Marklogic 集群配置问题 - (xdmp:wrong-host)
- javascript - 将 DOM 元素与 Canvas 混合
- macos - 我在 MacOS 上安装 Spark 时遇到问题,我该如何解决?
- java - Java Google SpreadSheet API,如何更新单元格以添加注释?