Extracting just a string element from a pandas dataframe

Hillary Sanders picture Hillary Sanders · Feb 26, 2015 · Viewed 15.4k times · Source

Okay, so say I have a pandas dataframe x, and I'm interested in extracting a value from it:

> x.loc[bar==foo]['variable_im_interested_in']

Let's say that returns the following, of type pandas.core.series.Series:

24    Boss
Name: ep_wb_ph_brand, dtype: object

But all I want is the string 'Boss'. Wrapping the first line of code in str() doesn't help either, I just get:

'24    Boss\nName: ep_wb_ph_brand, dtype: object'

How do I just extract the string?

Answer

ely picture ely · Feb 26, 2015

Based on your comments, this code is returning a length-1 pandas Series:

x.loc[bar==foo]['variable_im_interested_in']

If you assign this value to a variable, then you can just access the 0th element to get what you're looking for:

my_value_as_series = x.loc[bar==foo]['variable_im_interested_in']

# Assumes the index to get is number 0, but from your example, it might
# be 24 instead.
plain_value = my_value_as_series[0]

# Likewise, this needs the actual index value, not necessarily 0.
also_plain_value = my_value_as_series.ix[0]

# This one works with zero, since `values` is a new ndarray.
plain_value_too = my_value_as_series.values[0]

You don't have to assign to a variable to do this, so you could just write x.loc[bar==foo]['variable_im_interested_in'][0] (or similar for the other options), but cramming more and more accessor and fancy indexing syntax onto a single expression is usually a bad idea.

Also note that you can directly index the column of interest inside of the call to loc:

x.loc[bar==foo, 'variable_im_interested_in'][24]