在python中,我有这样一个数据帧:
pn
Out[249]:
a b c d e
2 4 8 12 131 127
cn.dtypes
Out[253]:
c1 object
c2 object
c3 object
dtype: object
我想选择pn["b"]作为字符串。
当我粘贴到下面时,我得到以下值:
pn["b"]
Out[257]:
2 8
Name: b, dtype: object
但我希望输出为字符串:
如以下x:
x="8"
x
Out[259]: '8'
我该怎么做?
如果有任何帮助,我将非常高兴。
谢谢你的帮助。
您可以使用astype
方法将列b
强制转换为字符串类型,然后使用iloc
访问特定行。
>>> import pandas as pd
>>>
>>> pn = pd.DataFrame({'b': [8]}, index=[2])
>>> pn
b
2 8
>>> pn['b'].astype(str).iloc[0]
'8'
>>> type(pn['b'].astype(str).iloc[0])
<class 'str'>