Convert floats to ints in Pandas? Convert floats to ints in Pandas? python python

Convert floats to ints in Pandas?


To modify the float output do this:

df= pd.DataFrame(range(5), columns=['a'])df.a = df.a.astype(float)dfOut[33]:          a0 0.00000001 1.00000002 2.00000003 3.00000004 4.0000000pd.options.display.float_format = '{:,.0f}'.formatdfOut[35]:   a0  01  12  23  34  4


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         D0  0.542447  0.949988  0.669239  0.8798871  0.068542  0.757775  0.891903  0.3845422  0.021274  0.587504  0.180426  0.574300>>> df[list("ABCD")] = df[list("ABCD")].astype(int)>>> df   A  B  C  D0  0  0  0  01  0  0  0  02  0  0  0  0

EDIT:

To handle missing values:

>>> df          A         B     C         D0  0.475103  0.355453  0.66  0.8693361  0.260395  0.200287   NaN  0.6170242  0.517692  0.735613  0.18  0.657106>>> df[list("ABCD")] = df[list("ABCD")].fillna(0.0).astype(int)>>> df   A  B  C  D0  0  0  0  01  0  0  0  02  0  0  0  0


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


matomo