如何在没有数据类型的情况下从DataFrame打印字符串值 [英] How to print string value from DataFrame without datatype
本文介绍了如何在没有数据类型的情况下从DataFrame打印字符串值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当我从数据框中显示一个单元格时,我会得到
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'
我如何只打印不带dtype
的值和不带字符串操作的名称?
How do I just print the value without dtype
and the name without string manipulations?
推荐答案
只需执行以下操作,返回的就是熊猫系列,因此您需要访问values
或name
属性:
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'
这篇关于如何在没有数据类型的情况下从DataFrame打印字符串值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文