How to Convert Floats to Ints in Pandas?

Spread the love

Problem –

You want to convert a float column to Int in pandas.

Solution –

Read a dataset to work with.

import pandas as pd

url = ''
df = pd.read_csv(url)

We can see that all of the columns are float type which can be changed to Int type. To do that we use the astype() method in pandas. But since here the columns contains NaN, we need to replace it with zeros before converting to int.

df.fillna(0, inplace=True)
df['apple'] = df['apple'].astype(int)
df['Orange'] = df['Orange'].astype(int)
df['Banana'] = df['Banana'].astype(int)
df['Mango'] = df['Mango'].astype(int)

Related Posts –

  1. Pandas – astype() – Change column data type in pandas
  2. How to Convert a String Column to Float in Pandas
  3. How to Convert String Column to Integer in Pandas

Rating: 1 out of 5.

Leave a Reply