python34 pandas 您所在的位置:网站首页 安徽农村古建筑 python34 pandas

python34 pandas

#python34 pandas| 来源: 网络整理| 查看: 265

更新:之后您无需转换您的值,您可以在阅读CSV时即时执行此操作:

In [165]: df=pd.read_csv(url, index_col=0, na_values=['(NA)']).fillna(0)

In [166]: df.dtypes

Out[166]:

GeoName object

ComponentName object

IndustryId int64

IndustryClassification object

Description object

2004 int64

2005 int64

2006 int64

2007 int64

2008 int64

2009 int64

2010 int64

2011 int64

2012 int64

2013 int64

2014 float64

dtype: object

如果需要将多个列转换为数字dtypes – 请使用以下技术:

样本来源DF:

In [271]: df

Out[271]:

id a b c d e f

0 id_3 AAA 6 3 5 8 1

1 id_9 3 7 5 7 3 BBB

2 id_7 4 2 3 5 4 2

3 id_0 7 3 5 7 9 4

4 id_0 2 4 6 4 0 2

In [272]: df.dtypes

Out[272]:

id object

a object

b int64

c int64

d int64

e int64

f object

dtype: object

将所选列转换为数字dtypes:

In [273]: cols = df.columns.drop('id')

In [274]: df[cols] = df[cols].apply(pd.to_numeric, errors='coerce')

In [275]: df

Out[275]:

id a b c d e f

0 id_3 NaN 6 3 5 8 1.0

1 id_9 3.0 7 5 7 3 NaN

2 id_7 4.0 2 3 5 4 2.0

3 id_0 7.0 3 5 7 9 4.0

4 id_0 2.0 4 6 4 0 2.0

In [276]: df.dtypes

Out[276]:

id object

a float64

b int64

c int64

d int64

e int64

f float64

dtype: object

PS如果要选择所有字符串(对象)列,请使用以下简单技巧:

cols = df.columns[df.dtypes.eq('object')]



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有