39

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.

mttk
  • 890
  • 2
  • 10
  • 11

4 Answers4

77

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.

EdChum
  • 376,765
  • 198
  • 813
  • 562
  • 2
    this is what I tend to use, but I must say that it still feels a little clunky. – El- Jun 21 '22 at 20:33
  • Yeah, it does feel clunky. Part of me wants to see a new feature made for conciseness in these kind of scenarios. Something like `df.sel(col: str|int = 0, row: str|int = 0)` where row can be supplied as a string indexer or row number, and column can be supplied as the column header or name, both of which defaulting to 0. `col` being the first positional argument because, imo, that suits a DataFrame better in this context (opposed to a Series, which is probably what someone is working with anyways if they'd benefit from something like `df.sel(row: str|int = 0, col: str|int = 0)`). – MrChadMWood May 24 '23 at 16:42
  • Edit: It exists `at` and `iat` such as `df.at[0, 'A']`. Read more: https://stackoverflow.com/questions/16729574/how-can-i-get-a-value-from-a-cell-of-a-dataframe – MrChadMWood May 24 '23 at 16:48
8

Late to the party here, but purchase_group['Column Name'].item() is now available and is cleaner than some other solutions

jesseWUT
  • 581
  • 4
  • 14
7

If purchase_group has single row then doing purchase_group = purchase_group.squeeze() would make it into a series so you could simply call purchase_group['Column_name'] to get your values

Yesh
  • 976
  • 12
  • 15
0

This method is intuitive; for example to get the first row (list from a list of lists) of values from the dataframe:

np.array(df)[0]
dna-data
  • 73
  • 5