Salih
Salih

Reputation: 719

Python Pandas Get A Sİngle Value From Dataframe

My input pandas dataframe look like below

df = pd.DataFrame([ ['AC', 'CompleteWeight', '0.2'],
['BB', 'CompleteWeight', '0.3'], ['RPL', 'CompleteWeight', '0.1'],
['IA', 'MultiPackageCompleteWeight', '0.2'],
['RPL', 'MultiPackageCompleteWeight', '0.5']],
columns=['Run Type', 'Name', 'Value'])
>>> df
  Run Type                        Name Value
0       AC              CompleteWeight   0.2
1       BB              CompleteWeight   0.3
2      RPL              CompleteWeight   0.1
3       IA  MultiPackageCompleteWeight   0.2
4      RPL  MultiPackageCompleteWeight   0.5
>>>

I want to get only 0.5 in "Value" column with using Column & Row names not the indexes.

Desired output is;

Desired =0.5

Could you please help me about this?

Upvotes: 2

Views: 445

Answers (1)

jezrael
jezrael

Reputation: 863741

Use DataFrame.loc with boolean indexing and then select first value:

mask = (df['Run Type'] == 'RPL') & (df['Name'] == 'MultiPackageCompleteWeight')
desired = df.loc[mask, 'Value'].values[0]
#alternative
#Desired = df.loc[mask, 'Value'].iat[0]
print(desired)
0.5

If possible RPL or MultiPackageCompleteWeight not exist:

desired = next(iter(df.loc[mask, 'Value']), 'no match')
print(desired)

Upvotes: 2

Related Questions