如何从Pandas中的Dataframe提取单元格值

2024-10-04 07:35:12 发布

您现在位置:Python中文网/ 问答频道 /正文

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

enter image description here

我想在instrument\u token列中搜索12295682并提取相关的tradingsymbol UBL20JANFUT。你知道吗

我该怎么做?你知道吗

提前谢谢


Tags: 数据tokeninstrumenttradingsymbolubl20janfut
3条回答

您可以使用:

list(df.loc[df['instrument_token'] == '12295682', 'tradingsymbol'])[0]

# UBL20JANFUT

可以使用^{}^{}作为按条件和列名筛选:

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')

另一个想法是按列instrument_token使用^{}fo索引:

df = df.set_index('instrument_token')

然后按^{}^{}

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

试试看

df[df['instrument_token'] == 12295682]['tradingsymbol']

相关问题 更多 >