为什么Pandas告诉我我有对象,尽管所选列中的每个项都是一个字符串 - 即使在显式转换之后也是如此.
这是我的DataFrame:
<class 'pandas.core.frame.DataFrame'>
Int64Index: 56992 entries, 0 to 56991
Data columns (total 7 columns):
id 56992 non-null values
attr1 56992 non-null values
attr2 56992 non-null values
attr3 56992 non-null values
attr4 56992 non-null values
attr5 56992 non-null values
attr6 56992 non-null values
dtypes: int64(2), object(5)
Run Code Online (Sandbox Code Playgroud)
其中五个是dtype object.我明确地将这些对象转换为字符串:
for c in df.columns:
if df[c].dtype == object:
print "convert ", df[c].name, " to string"
df[c] = df[c].astype(str)
Run Code Online (Sandbox Code Playgroud)
然后,df["attr2"]仍然有dtype object,虽然type(df["attr2"].ix[0]揭示str,这是正确的. …
我在Python 2.7中使用Pandas'ver 0.12.0',并拥有如下数据帧:
df = pd.DataFrame({'id' : [123,512,'zhub1', 12354.3, 129, 753, 295, 610],
'colour': ['black', 'white','white','white',
'black', 'black', 'white', 'white'],
'shape': ['round', 'triangular', 'triangular','triangular','square',
'triangular','round','triangular']
}, columns= ['id','colour', 'shape'])
Run Code Online (Sandbox Code Playgroud)
该id系列包含一些整数和字符串.它dtype默认是object.我想将所有内容转换id为字符串.我试过astype(str),它产生下面的输出.
df['id'].astype(str)
0 1
1 5
2 z
3 1
4 1
5 7
6 2
7 6
Run Code Online (Sandbox Code Playgroud)
1)如何将所有元素转换id为String?
2)我最终将id用于索引数据帧.与具有整数索引相比,数据帧中的String索引会减慢速度吗?
我正在尝试使用 pandas 将数据框中的对象转换为字符串。有以下数据:
particulars
NWCLG 545627 ASDASD KJKJKJ ASDASD
TGS/ASDWWR42045645010009 2897/SDFSDFGHGWEWER
dtype:object
Run Code Online (Sandbox Code Playgroud)
在尝试使用astype()[使用 str、|S、|S32、|S80] 类型或直接使用 str 函数将详细信息列从对象转换为字符串时,它不会在字符串(保留对象)中转换,并且对于str 方法[替换'/' 和 ' ']它说AttributeError: 'DataFrame' object has no attribute 'str'
使用熊猫0.23.4