而不是做:
for col in df.columns :
df[col]= df[col].astype('category')
Run Code Online (Sandbox Code Playgroud)
正在这样做:
dtype0= {'brand': np.dtype('int64'),
'category': np.dtype('int64'),
'chain': np.dtype('int64'),
'company': np.dtype('int64'),
'date': np.dtype('O'),
'dept': pandas.types.dtypes.CategoricalDtype,
'id': np.dtype('int64')}
df= df.astype(dtype0)
Run Code Online (Sandbox Code Playgroud)
但是,它不起作用。只是想知道如何使用字典更改为类别。
先前的答案不正确。我们可以在创建数据框后进行转换。
解决方法是(记录在这里的其他人):熊猫0.19.1
dtype0= {'brand': 'int64',
'category': 'int64',
'chain': 'int64',
'company': 'int64',
'date': 'str',
'dept': 'category',
'id': 'int64'}
df= df.astype(dtype0)
Run Code Online (Sandbox Code Playgroud)
铸造作品在这里。
ℕʘʘ*_*ḆḽḘ -6
你只能在读入数据时才能做到这一点
data = pd.read_csv('mypath.csv', dtypes = mydict)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2701 次 |
| 最近记录: |