简体   繁体   中英

How to print string value from DataFrame without datatype

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?

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'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM