在Datafrane Pandas中将Object dtype列转换为Number Dtype [英] Convert an Object dtype column to Number Dtype in a datafrane Pandas

查看:181
本文介绍了在Datafrane Pandas中将Object dtype列转换为Number Dtype的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试回答此问题

Trying to answer this question Get List of Unique String per Column we ran into a different problem from my dataset. When I import this CSV file to the dataframe every column is OBJECT type, we need to convert the columns that are just number to real (number) dtype and those that are not number to String dtype.

有没有办法做到这一点?

Is there a way to achieve this?

从此处下载数据示例

我尝试了以下文章> Pandas:更改列的数据类型但没有用.

I have tried following code from following article Pandas: change data type of columns but did not work.

df = pd.DataFrame(a, columns=['col1','col2','col3'])

一如既往地感谢您的帮助

As always thanks for your help

推荐答案

选项1
apply

Option 1
use pd.to_numeric in an apply

df.apply(pd.to_numeric, errors='ignore')

选项2
df.values.ravel

Option 2
use pd.to_numeric on df.values.ravel

cvrtd = pd.to_numeric(df.values.ravel(), errors='coerce').reshape(-1, len(df.columns))
pd.DataFrame(np.where(np.isnan(cvrtd), df.values, cvrtd), df.index, df.columns)


注意
这些并不完全相同.对于某些包含混合值的列,选项2会转换其内容,而选项2会将该列中的所有内容保留为对象.查看您的文件,我将选择选项1.


Note
These are not exactly the same. For some column that contains mixed values, option 2 converts what it can while option 2 leaves everything in that column an object. Looking at your file, I'd choose option 1.

时间

Timing

df = pd.read_csv('HistorianDataSample/HistorianDataSample.csv', skiprows=[1, 2])

这篇关于在Datafrane Pandas中将Object dtype列转换为Number Dtype的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆