Josef
Josef

Reputation: 2726

Read dataframe values by certain creteria

I'm reading some data using panadas and this is dataframe:

PARAMETER VALUE
0 Param1 1.2
1 Param2 5.0
2 Param3 9.3
3 Param4 30
4 Param5 1500

What would be the best way to access values by parameter name? For example I need value of Param4 is there any way to say like read['Param'].value?

Upvotes: 1

Views: 98

Answers (2)

Rohith Sureddi
Rohith Sureddi

Reputation: 1

  1. I dont think there is a way to read with conditions.

  2. But, you can do it using the following code.

     df[df['PARAMETER'] == 'Param4']['VALUE']
    

Upvotes: 0

sophocles
sophocles

Reputation: 13821

One way would be using loc like:

float(df.loc[df['PARAMETER']=='Param4']['VALUE']) # locate col PARAMETER and get VALUE
Out[81]: 30.0

# Or
df.loc[df['PARAMETER']=='Param4']['VALUE'].values
Out[94]: array([30.])

Another way would be to create a dict and access them like:

# Using a dictionary
d = dict(zip(df.PARAMETER,df.VALUE))
d['Param4']
Out[82]: 30.0

d['Param3']
Out[90]: 9.3

Upvotes: 1

Related Questions