Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extract value from single row of pandas DataFrame

Tags:

python

pandas

I have a dataset in a relational database format (linked by ID's over various .csv files).

I know that each data frame contains only one value of an ID, and I'd like to know the simplest way to extract values from that row.

What I'm 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 is bothering me as it seems ugly, however I'm not sure what is the workaround. The grouping (I guess) assumes that there might be multiple values and returns a <class 'pandas.core.frame.DataFrame'> object, while I'd like just a row returned.

like image 496
mttk Avatar asked Jul 21 '15 10:07

mttk


People also ask

How do you select a specific value in a DataFrame?

Select Data Using Location Index (. This means that you can use dataframe. iloc[0:1, 0:1] to select the cell value at the intersection of the first row and first column of the dataframe. You can expand the range for either the row index or column index to select more data.

How do you access a particular element in a data frame?

By using at and iat attributes We can also access a single value of a DataFrame with the help of “at” and “iat” attributes. Access a single value by row/column name. At and iat take two arguments. If we pass only one argument, then it will generate an error.

How do you access a row in a DataFrame?

To access a group of rows in a Pandas DataFrame, we can use the loc() method. For example, if we use df. loc[2:5], then it will select all the rows from 2 to 5.


Video Answer


1 Answers

If you want just the value and not a df/series then call values and index the first element [0] so just:

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

will work.

like image 55
EdChum Avatar answered Oct 04 '22 12:10

EdChum