pandas 中同时得到value和对应的index

本文介绍如何在Python pandas库中获取Series数据的最大值及其对应的索引标签,提供了两种实现方法,并展示了具体的输出结果。

有时对于 pd.Series, 得到最大值的同时 也需要得到对应的标签,示例:

## 构造 pd.Series
d = [12, 4, 4, 8, 8, 2, 8, 10, 5, 4]
ind = ['FERRARI 612 Scaglietti',
 'HONDA Accord',
 'SUBARU Forester',
 'FORD F150 STX SE',
 'FORD F150',
 'MAZDA RX-8',
 'BMW 650ci Convertible',
 'BMW M6',
 'VOLVO S60',
 'HONDA Element']

test = pd.Series(data=d, index=ind)

## 获取最大值和对应的标签 (方便在程序中使用)
test.max(), test.idxmax()

## output
(12, 'FERRARI 612 Scaglietti')

另外一种方式:

a = test[test == test.max()]
a
## output
FERRARI 612 Scaglietti    12
dtype: int64

for k,v in a.items():
    print(k)
    print(v)
## output
FERRARI 612 Scaglietti
12
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值