Retrieving only string element from pandas data frame

Well, let's say I have pandas dataframe x, and I am interested in extracting a value from it:

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

Let's say that it 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". The shell of the first line of code str()doesn’t help either, I just get:

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

How can I extract a string?

+4
source share
3 answers

Based on your comments, this code returns a length of-1 pandas Series:

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

If you assign this value to a variable, you can simply access the 0th element to get what you are 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]

, x.loc[bar==foo]['variable_im_interested_in'][0] ( ), .

, loc:

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

string.split.

>>> s = '24    Boss\nName: ep_wb_ph_brand, dtype: object'
>>> s.split()[1]
'Boss'
+1

Code to get the last value of the array (run on a Jupyter laptop marked with> s):

> import pandas
> df = pandas.DataFrame(data=['a', 'b', 'c'], columns=['name'])
> df
    name
0   a
1   b
2   c
> df.tail(1)['name'].values[0]
'c'
0
source

All Articles