2016-04-23 199 views
12

我用下面的df工作:大熊貓:to_numeric多個列

c.sort_values('2005', ascending=False).head(3) 
    GeoName  ComponentName IndustryId IndustryClassification Description  2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 
37926 Alabama  Real GDP by state 9 213  Support activities for mining 99 98 117  117  115  87 96 95 103  102  (NA) 
37951 Alabama  Real GDP by state 34 42 Wholesale trade  9898 10613 10952 11034 11075 9722 9765 9703 9600 9884 10199 
37932 Alabama  Real GDP by state 15 327  Nonmetallic mineral products manufacturing 980  968  940  1084 861  724  714  701  589  641  (NA) 

我要強制數字上的所有年:

c['2014'] = pd.to_numeric(c['2014'], errors='coerce') 

有一種簡單的方法來做到這個還是我必須全部輸入?

回答

12

UPDATE:你不需要你的價值觀轉換後,你可以做到這一點上的讀你的CSV時-fly

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 - 使用下面的技巧:如果你

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:

樣品來源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要選擇全部stringobject)列使用以下簡單的技巧:

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

感謝MaxU,對於你的答案:) –

+0

@MichaelPerdue中,很高興幫助:) – MaxU

+0

爲了完整:你也可以做轉換初始化數據幀例如,當上了飛:pd.DataFrame(DataList控件,D型=浮動),它會將所有字段轉換爲可能的字段(並保持其他字段不變) – poppie

8

您可以使用:

print df.columns[5:] 
Index([u'2004', u'2005', u'2006', u'2007', u'2008', u'2009', u'2010', u'2011', 
     u'2012', u'2013', u'2014'], 
     dtype='object') 

for col in df.columns[5:]: 
    df[col] = pd.to_numeric(df[col], errors='coerce') 

print df 
     GeoName  ComponentName IndustryId IndustryClassification \ 
37926 Alabama Real GDP by state   9      213 
37951 Alabama Real GDP by state   34      42 
37932 Alabama Real GDP by state   15      327 

             Description 2004 2005 2006 2007 \ 
37926    Support activities for mining 99  98 117 117 
37951       Wholesale trade 9898 10613 10952 11034 
37932 Nonmetallic mineral products manufacturing 980 968 940 1084 

     2008 2009 2010 2011 2012 2013  2014 
37926 115 87 96 95 103 102  NaN 
37951 11075 9722 9765 9703 9600 9884 10199.0 
37932 861 724 714 701 589 641  NaN 

另一種解決方案與filter

print df.filter(like='20') 
     2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 
37926 99  98 117 117 115 87 96 95 103 102 (NA) 
37951 9898 10613 10952 11034 11075 9722 9765 9703 9600 9884 10199 
37932 980 968 940 1084 861 724 714 701 589 641 (NA) 

for col in df.filter(like='20').columns: 
    df[col] = pd.to_numeric(df[col], errors='coerce') 
print df 
     GeoName  ComponentName IndustryId IndustryClassification \ 
37926 Alabama Real GDP by state   9      213 
37951 Alabama Real GDP by state   34      42 
37932 Alabama Real GDP by state   15      327 

             Description 2004 2005 2006 2007 \ 
37926    Support activities for mining 99  98 117 117 
37951       Wholesale trade 9898 10613 10952 11034 
37932 Nonmetallic mineral products manufacturing 980 968 940 1084 

     2008 2009 2010 2011 2012 2013  2014 
37926 115 87 96 95 103 102  NaN 
37951 11075 9722 9765 9703 9600 9884 10199.0 
37932 861 724 714 701 589 641  NaN 
+0

謝謝,這工作:) –

+0

很高興能幫助你!祝你好運! – jezrael

14

另一種方法是使用apply,一個內膽:

cols = ['col1', 'col2', 'col3'] 
data[cols] = data[cols].apply(pd.to_numeric, errors='coerce', axis=1) 
0

如果你正在尋找一個範圍欄,你可以試試這個:以上

df.iloc[7:] = df.iloc[7:].astype(float) 

的例子將轉換類型浮動,所有的列從第七個開始到結束。你當然可以使用不同的類型或不同的範圍。

我認爲這是有用的,當你有一個大範圍的列轉換和很多行。它不會讓你自己走過每一排 - 我相信numpy會更有效率地做到這一點。

只有當你知道所有需要的列只包含數字時它纔有用 - 它不會將「壞值」(如字符串)更改爲NaN。