Dan*_*rty 5 python types pandas
给定以下数据框:
DF = pd.DataFrame({'COL1': ['A', 'B', 'C', 'D','D','D'],
'mixed': [2016.0, 2017.0, 'sweatervest', 20, 209, 21]})
DF
COL1 mixed
0 A 2016.0
1 B 2017.0
2 C sweatervest
3 D 20
4 D 209
5 D 21
Run Code Online (Sandbox Code Playgroud)
我想将“ mixed”转换为对象,以便所有数字都是整数(字符串),而所有字符串当然都是字符串。所需的输出如下:
COL1 mixed
0 A 2016
1 B 2017
2 C sweatervest
3 D 20
4 D 209
5 D 21
Run Code Online (Sandbox Code Playgroud)
背景信息:
最初,“混合”是从主要由数字组成的CSV数据帧的一部分,到处都有一些字符串。当我尝试将其转换为字符串时,某些数字最后以'.0'结尾。
提前致谢...
尝试:
DF['mixed']=DF.mixed.astype(object)
Run Code Online (Sandbox Code Playgroud)
这导致:
DF['mixed']
0 2016
1 2017
2 sweatervest
3 20
4 209
5 21
Name: mixed, dtype: object
Run Code Online (Sandbox Code Playgroud)