python - Convert floats to ints in Pandas?

ID : 10172

viewed : 24

Tags : pythonpandasfloating-pointintegerdatasetpython

Top 5 Answer for python - Convert floats to ints in Pandas?

vote vote

97

To modify the float output do this:

df= pd.DataFrame(range(5), columns=['a']) df.a = df.a.astype(float) df  Out[33]:            a 0 0.0000000 1 1.0000000 2 2.0000000 3 3.0000000 4 4.0000000  pd.options.display.float_format = '{:,.0f}'.format df  Out[35]:     a 0  0 1  1 2  2 3  3 4  4 
vote vote

82

Use the pandas.DataFrame.astype(<type>) function to manipulate column dtypes.

>>> df = pd.DataFrame(np.random.rand(3,4), columns=list("ABCD")) >>> df           A         B         C         D 0  0.542447  0.949988  0.669239  0.879887 1  0.068542  0.757775  0.891903  0.384542 2  0.021274  0.587504  0.180426  0.574300 >>> df[list("ABCD")] = df[list("ABCD")].astype(int) >>> df    A  B  C  D 0  0  0  0  0 1  0  0  0  0 2  0  0  0  0 

EDIT:

To handle missing values:

>>> df           A         B     C         D 0  0.475103  0.355453  0.66  0.869336 1  0.260395  0.200287   NaN  0.617024 2  0.517692  0.735613  0.18  0.657106 >>> df[list("ABCD")] = df[list("ABCD")].fillna(0.0).astype(int) >>> df    A  B  C  D 0  0  0  0  0 1  0  0  0  0 2  0  0  0  0 
vote vote

78

Considering the following data frame:

>>> df = pd.DataFrame(10*np.random.rand(3, 4), columns=list("ABCD")) >>> print(df) ...           A         B         C         D ... 0  8.362940  0.354027  1.916283  6.226750 ... 1  1.988232  9.003545  9.277504  8.522808 ... 2  1.141432  4.935593  2.700118  7.739108 

Using a list of column names, change the type for multiple columns with applymap():

>>> cols = ['A', 'B'] >>> df[cols] = df[cols].applymap(np.int64) >>> print(df) ...    A  B         C         D ... 0  8  0  1.916283  6.226750 ... 1  1  9  9.277504  8.522808 ... 2  1  4  2.700118  7.739108 

Or for a single column with apply():

>>> df['C'] = df['C'].apply(np.int64) >>> print(df) ...    A  B  C         D ... 0  8  0  1  6.226750 ... 1  1  9  9  8.522808 ... 2  1  4  2  7.739108 
vote vote

63

This is a quick solution in case you want to convert more columns of your pandas.DataFrame from float to integer considering also the case that you can have NaN values.

cols = ['col_1', 'col_2', 'col_3', 'col_4'] for col in cols:    df[col] = df[col].apply(lambda x: int(x) if x == x else "") 

I tried with else x) and else None), but the result is still having the float number, so I used else "".

vote vote

56

To convert all float columns to int

>>> df = pd.DataFrame(np.random.rand(5, 4) * 10, columns=list('PQRS')) >>> print(df) ...     P           Q           R           S ... 0   4.395994    0.844292    8.543430    1.933934 ... 1   0.311974    9.519054    6.171577    3.859993 ... 2   2.056797    0.836150    5.270513    3.224497 ... 3   3.919300    8.562298    6.852941    1.415992 ... 4   9.958550    9.013425    8.703142    3.588733  >>> float_col = df.select_dtypes(include=['float64']) # This will select float columns only >>> # list(float_col.columns.values)  >>> for col in float_col.columns.values: ...     df[col] = df[col].astype('int64')  >>> print(df) ...     P   Q   R   S ... 0   4   0   8   1 ... 1   0   9   6   3 ... 2   2   0   5   3 ... 3   3   8   6   1 ... 4   9   9   8   3 

Top 3 video Explaining python - Convert floats to ints in Pandas?

Related QUESTION?