简体   繁体   English

如何在没有数据类型的情况下从DataFrame打印字符串值

[英]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? 如何在不使用字符串操作的情况下打印没有dtype和名称的值?

Just do the following, what is returned is a pandas Series so you need to acess either the values or the name attribute: 只需执行以下操作,返回的是一个pandas系列,因此您需要访问valuesname属性:

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'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将带字符串的数据帧连接到带Unicode的数据帧并规范化数据类型 - How to Concat a DataFrame with String to a DataFrame with Unicode and normalize datatype Python 2.7:如何从pandas数据帧中的字符串中识别唯一字符串,并根据结果在指定列中打印指定值? - Python 2.7: How to identify unique string from string in pandas dataframe and print designated value in a specified column based on the result? 从没有Unicode的键中打印字典值[u'STRING] - Print Dictionary Value From Key Without The Unicode [u'STRING] 如何在 1 个打印语句中打印字符串和 dataframe? - How to print a string and a dataframe in 1 print statement? 如何从print()编写的字符串中获取Python pandas DataFrame? - How to get Python pandas DataFrame from string written by print()? 如何将字符串的值转换为其特定的数据类型? '5' -> 5 或 'a' -> a - How to convert value of a String into it's specific datatype? '5' -> 5 OR 'a' -> a 如何在没有 b`` 字符串的情况下打印? - How to print without the b`` string? 如何在没有索引的情况下打印 Pandas DataFrame - How to print pandas DataFrame without index 如何在不包含 None 的情况下在一行中打印数据框? - How to print a dataframe in oneline without including None? 如何打印没有列名和索引的 dataframe - How to print a dataframe without the name of the columns and the index
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM