在加载的 excel 文件上使用 df.info 方法后,我得到以下数据<class 'pandas.core.frame.DataFrame'>RangeIndex: 30000 entries, 1 to 30000Data columns (total 25 columns): # Column Non-Null Count Dtype --- ------ -------------- ----- 0 Unnamed: 0 30000 non-null object 1 X1 30000 non-null object 2 X2 30000 non-null object 3 X3 29669 non-null object 4 X4 29677 non-null object 5 X5 30000 non-null object 6 X6 30000 non-null object 7 X7 30000 non-null object 8 X8 30000 non-null object 9 X9 30000 non-null object 10 X10 30000 non-null object 11 X11 30000 non-null object 12 X12 30000 non-null object 13 X13 30000 non-null object 14 X14 30000 non-null object 15 X15 30000 non-null object 16 X16 30000 non-null object 17 X17 30000 non-null object 18 X18 30000 non-null object 19 X19 30000 non-null object 20 X20 30000 non-null object 21 X21 30000 non-null object 22 X22 30000 non-null object 23 X23 30000 non-null object 24 Y 30000 non-null objectdtypes: object(25)memory usage: 2.9+ MB我不知道为什么所有数据类型都是对象,尽管它们中的大多数都有数值 如何修复我的数据集的数据类型
2 回答

慕姐8265434
TA贡献1813条经验 获得超2个赞
让我们试试to_numeric
df = pd.DataFrame({'1':['1','2'],'2':['a','b']})
df = df.apply(pd.to_numeric,errors='ignore')
查看
df.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 2 entries, 0 to 1
Data columns (total 2 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 1 2 non-null int64
1 2 2 non-null object
dtypes: int64(1), object(1)
memory usage: 88.0+ bytes

神不在的星期二
TA贡献1963条经验 获得超6个赞
尝试例如:
df['X1'] = df['X1'].astype(str).astype(int)
如果要格式化所有列,请尝试:
df = df.astype(int)
这是因为,当您导入.csv
文件时,大部分列都被转换为对象。
添加回答
举报
0/150
提交
取消