You want to convert a float column to Int in pandas.
Read a dataset to work with.
import pandas as pd url = 'https://raw.githubusercontent.com/bprasad26/lwd/master/data/fruit_prices.csv' df = pd.read_csv(url) df.head()
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 –
- Pandas – astype() – Change column data type in pandas
- How to Convert a String Column to Float in Pandas
- How to Convert String Column to Integer in Pandas