如何从Pandas的数据框中提取单元格值 [英] How to extract a cell value from a Dataframe in Pandas

查看:48
本文介绍了如何从Pandas的数据框中提取单元格值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含3列的数据框,如下所示:

I have a dataframe with 3 columns, like this:

我想在instrument_token列中搜索12295682,并提取相关的交易代码UBL20JANFUT.

I want to search for 12295682 in instrument_token column and extract the related tradingsymbol UBL20JANFUT.

我该怎么做?

预先感谢

推荐答案

您可以使用

You can use boolean indexing with DataFrame.loc for filter by condition and by column name:

s = df.loc[df['instrument_token'].eq(12295682), 'tradingsymbol']
#alternative
s = df.loc[df['instrument_token'] == 12295682, 'tradingsymbol']

然后获取 Series 的第一个值:

a = s.iat[0]
a = s.iloc[0]
a = s.tolist()[0]
a = s.to_array()[0]
#general solution if not match condition and select first value failed
a = next(iter(s), 'no match')

另一个想法是使用 DataFrame.set_index 按列 instrument_token 的索引:

Another idea is use DataFrame.set_index fo index by column instrument_token:

df = df.set_index('instrument_token')

然后选择 DataFrame.loc DataFrame.at :

a = df.loc[12295682, 'tradingsymbol']
a = df.at[12295682, 'tradingsymbol']

这篇关于如何从Pandas的数据框中提取单元格值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆