Abhi
Abhi

Reputation: 163

Selecting Pandas dataframe column

I am trying to use pandas data-frame as a parameter table which is loaded in the beginning of my application run.

Structure of the csv that is being loaded into the data-frame is as below :

param_name,param_value
source_dir,C:\Users\atiwari\Desktop\EDIFACT\source_dir

So the column names would be param_name and param_values.

How do i go about selecting the value from param_value where param_name == 'source_dir'?

I tried the below but it returns a data-frame with index not a string value:

param_df.loc[param_df['param_name']=='source_dir']['param_value']

Upvotes: 2

Views: 84

Answers (1)

jezrael
jezrael

Reputation: 862481

It return Series:

s = param_df.loc[param_df['param_name']=='source_dir', 'param_value']

But if need DataFrame:

df = param_df.loc[param_df['param_name']=='source_dir', ['param_value']]

For scalar need convert Series by selecting by [] - select first value by 0. Also works iat.

Series.item need Series with values else get error if empty Series:

val = s.values[0]

val = s.iat[0]

val = s.item()

Upvotes: 2

Related Questions