Using fillna, downcast and pandas

I was looking for something that helped me understand the keyword argument downcastin a class method DataFrame.fillna. Please provide an example to help my and all students: http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.fillna.html

Also, if you can say a word or two about setting the type in a column by column with values NaNor even NoneTypein a column and how to handle such ordinary things. And what is the difference between the two.

Many thanks!

+4
source share
1 answer

Even though doc says:

downcast : dict, default is None

a dict of item- > dtype , , , "infer", (, float64 int64, )

dict downcast, AssertionError("dtypes as dict is not supported yet")

downcast='infer', pandas , , float . : 10000, .

In [1]: import pandas as pd
   ...: import numpy as np
   ...: df = pd.DataFrame([[3.14,9999.9,10000.1,200000.2],[2.72,9999.9,10000.1,300000.3]], columns=list("ABCD"))
   ...: df.dtypes
   ...: 
Out[1]: 
A    float64
B    float64
C    float64
D    float64
dtype: object

In [2]: df
Out[2]: 
      A       B        C         D
0  3.14  9999.9  10000.1  200000.2
1  2.72  9999.9  10000.1  300000.3

In [3]: dff=df.fillna(0, downcast='infer')
   ...: dff.dtypes
   ...: 
Out[3]: 
A    float64
B    float64
C      int64
D      int64
dtype: object

In [4]: dff
Out[4]: 
      A       B      C       D
0  3.14  9999.9  10000  200000
1  2.72  9999.9  10000  300000
+2

All Articles