python - Pandas: unable to change column data type -


i following advice here change column data type of pandas dataframe. however, not seem work if reference columns index numbers instead of column names. there way correctly?

in [49]: df.iloc[:, 4:].astype(int) out[49]:  &ltclass 'pandas.core.frame.dataframe'&gt int64index: 5074 entries, 0 5073 data columns (total 3 columns): 5    5074  non-null values 6    5074  non-null values 7    5074  non-null values dtypes: int64(3)   in [50]: df.iloc[:, 4:] = df.iloc[:, 4:].astype(int)  in [51]: df out[51]:  &ltclass 'pandas.core.frame.dataframe'&gt int64index: 5074 entries, 0 5073 data columns (total 7 columns): 1    5074  non-null values 2    5074  non-null values 3    5074  non-null values 4    5074  non-null values 5    5074  non-null values 6    5074  non-null values 7    5074  non-null values dtypes: object(7)   in [52]:  

do this

in [49]: df = dataframe([['1','2','3','.4',5,6.,'foo']],columns=list('abcdefg'))  in [50]: df out[50]:      b  c   d  e  f    g 0  1  2  3  .4  5  6  foo  in [51]: df.dtypes out[51]:      object b     object c     object d     object e      int64 f    float64 g     object dtype: object 

need assign columns one-by-one

in [52]: k, v in df.iloc[:,0:4].convert_objects(convert_numeric=true).iteritems():     df[k] = v    ....:       in [53]: df.dtypes out[53]:       int64 b      int64 c      int64 d    float64 e      int64 f    float64 g     object dtype: object 

convert objects right thing, easiest this

in [54]: df = dataframe([['1','2','3','.4',5,6.,'foo']],columns=list('abcdefg'))  in [55]: df.convert_objects(convert_numeric=true).dtypes out[55]:       int64 b      int64 c      int64 d    float64 e      int64 f    float64 g     object dtype: object 

assigning via df.iloc[:,4:] series on right-hand side copies data changing type needed, think should work in theory, suspect hitting obscure bug prevents object dtype changing real (meaning int/float) dtype. should raise now.

heres issue track this: https://github.com/pydata/pandas/issues/4312


Comments

Popular posts from this blog

html5 - What is breaking my page when printing? -

html - Unable to style the color of bullets in a list -

c# - must be a non-abstract type with a public parameterless constructor in redis -