Extract value from one row pandas DataFrame

I have a dataset in a relational database format (linked by an ID across various .csv files).

I know that each data frame only contains one ID value, and I would like to know the simplest way to extract values ​​from that string.

What I am doing now:

# the group has only one element
purchase_group = purchase_groups.get_group(user_id)
price = list(purchase_group['Column_name'])[0]

      

The third row bothers me as it seems ugly, however I'm not sure what the workaround is. Grouping (I think) assumes there can be multiple values ​​and returns an object <class 'pandas.core.frame.DataFrame'>

, while I would like to return only a string.

+5


source to share


2 answers


If you just want a value and not a df / series then call values

and index the first element [0]

to just:

price = purchase_group['Column_name'].values[0]

      



will work.

+14


source


If purchase_group

there is only one line for, then purchase_group = purchase_group.squeeze()

it turns into a series when executed , so you can simply call purchase_group['Column_name']

to get your values



0


source







All Articles