Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to print string value from DataFrame without datatype

Tags:

python

pandas

When I display a cell from a dataframe, I get

df[df.a==1]['b']
Out[120]: 
0    2
Name: b, dtype: int64

However, when I want to convert it to string, I get

str(df[df.a==1]['b'])
Out[124]: '0    2\nName: b, dtype: int64'

How do I just print the value without dtype and the name without string manipulations?

like image 330
desmond Avatar asked Mar 06 '15 20:03

desmond


1 Answers

Just do the following, what is returned is a pandas Series so you need to acess either the values or the name attribute:

In [2]:

df = pd.DataFrame({'a':np.arange(5), 'b':np.random.randn(5)})
df
Out[2]:
   a         b
0  0 -1.795051
1  1  1.579010
2  2  1.908378
3  3  1.814691
4  4 -0.470358

In [16]:

type(df[df['a']==2]['b'])
Out[16]:
pandas.core.series.Series

In [9]:

df[df['a']==2]['b'].values[0]
Out[9]:
1.9083782154318822

In [15]:

df.loc[df['a']==2,'b'].name
Out[15]:
'b'
like image 138
EdChum Avatar answered Oct 16 '22 12:10

EdChum