I have a dataframe with 3 columns, like this:
I want to search for 12295682 in instrument_token column and extract the related tradingsymbol UBL20JANFUT.
How do I do that?
thanks in advance
You can extract a column of pandas DataFrame based on another value by using the DataFrame. query() method. The query() is used to query the columns of a DataFrame with a boolean expression.
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']
And then get first value of 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')
Another idea is use DataFrame.set_index
fo index by column instrument_token
:
df = df.set_index('instrument_token')
Anf then select by DataFrame.loc
or
DataFrame.at
:
a = df.loc[12295682, 'tradingsymbol']
a = df.at[12295682, 'tradingsymbol']
You can use:
list(df.loc[df['instrument_token'] == '12295682', 'tradingsymbol'])[0]
# UBL20JANFUT
try
df[df['instrument_token'] == 12295682]['tradingsymbol']
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With