In this post, we are going to learn how to Convert multiple float columns to int Pandas Dataframe multiple float columns to int Python pandas with code examples and how to convert the entire dataframe column float to int
1. astype() to Convert multiple float columns to int Pandas Dataframe
The astype() method allows us to pass datatype explicitly, even we can use Python dictionary to change multiple datatypes at a time, where keys specify the column and values specify the new datatype.
In this example we have convert single dataframe column to float to int by using astype() method
Python Program to convert column float to int
import pandas as pd
Student_dict = {
'Name': ['Jack', 'Rack', 'Max'],
'Marks':[100.5,100.7, 10.78],
'Subject': ['Math', 'Math', 'Music']
}
dfobj = pd.DataFrame(Student_dict)
dfobj['Marks'] = dfobj['Marks'].astype(int)
print ('\n float to int:\n',dfobj)
print ('\n float to int converted dataframe :\n',dfobj.dtypes)
Output
float to int:
Name Marks Subject
0 Jack 100 Math
1 Rack 100 Math
2 Max 10 Music
float to int converted dataframe :
Name object
Marks int32
Subject object
dtype: object
2. Convert multiple float columns to int Pandas Dataframe
In this example, we are converting multiple columns that have float values to int by using the astype(int) method of the Pandas library by passing a dictionary.We are using a Python dictionary to change multiple columns of datatype Where keys specify the column name and values specify a new datatype.
Python Program to convert column float to int
import pandas as pd
Student_dict = {
'Name': ['Jack', 'Rack', 'Max'],
'Marks':[100.5,100.7, 10.78],
'Fee':[10.5,200.70, 105.78],
'Subject': ['Math', 'Math', 'Music']
}
dfobj = pd.DataFrame(Student_dict)
dict_columns_type = {'Marks': int,
'Fee': int
}
dfobj = dfobj.astype(dict_columns_type)
print('dataframe int to float:\n',dfobj)
print(f'\n {dfobj.dtypes}')
Output
dataframe int to float:
Name Marks Fee Subject
0 Jack 100 10 Math
1 Rack 100 200 Math
2 Max 10 105 Music
Name object
Marks int32
Fee int32
Subject object
dtype: object
3. Convert multiple dataframe columns nan value float to int
Sometimes we have dataframe columns that contain both NAN and float values. We have to convert float values to int and NAN values to zero. We are using the numpy library to fill the np. nan values to dataframe.
- We will use the numpy library to specific the NAN values
- The astype() method to convert float to int
Python Program to convert mutiple nan float to int
import pandas as pd
import numpy as np
Student_dict = {
'Age': [np.nan, 3.6, np.nan],
'Marks':[100.5,100.7, 10.78],
'Fee':[np.nan,200.70, np.nan]
}
dfobj = pd.DataFrame(Student_dict)
dict_columns_type = {'Age':int,
'Marks': int,
'Fee': int
}
dfobj = dfobj.fillna(0).astype(dict_columns_type)
print('dataframe column float to int :\n',dfobj)
print(f'\n {dfobj.dtypes}')
Output
dataframe column float to int :
Age Marks Fee
0 0 100 0
1 3 100 200
2 0 10 0
Age int32
Marks int32
Fee int32
dtype: object
4. Convert entire dataframe to float to int
To convert an entire dataframe columns float to int we just need to call the astype() method by using the dataframe object and specifying the datatype in which we want to convert.
Python Program to convert entire dataframe float to int
import pandas as pd
Student_dict = {
'Age': [2.5, 3.6, 3.7],
'Marks':[100.5,100.7, 10.78],
'Fee':[10.5,200.70, 105.78]
}
dfobj = pd.DataFrame(Student_dict)
dfobj = dfobj.astype(int)
print('dataframe column float to int :\n',dfobj)
print(f'\n {dfobj.dtypes}')
Output
dataframe column float to int :
Age Marks Fee
0 2 100 10
1 3 100 200
2 3 10 105
Age int32
Marks int32
Fee int32
dtype: object
5. Convert multiple float columns to int pandas dataframe
In this example, we are converting mutiple columns integer to float by using apply() function. We are converting ‘Marks’ and ‘Fee’.
import pandas as pd
import numpy as np
Student_dict = {
'Age': [2.5, 3.6, 3.7],
'Marks':[100.5,100.7, 10.78],
'Fee':[10.5,200.70, 105.78]
}
df = pd.DataFrame(Student_dict)
print('Existing datatype:\n',df.dtypes)
df['Marks'] = df['Marks'].apply(np.int64)
df['Fee'] = df['Fee'].astype(np.int64)
print('\n',df,'\n')
print('\n datatype after conversio:\n',df.dtypes)
Output
Existing datatype:
Age float64
Marks float64
Fee float64
dtype: object
Age Marks Fee
0 2.5 100 10
1 3.6 100 200
2 3.7 10 105
datatype after conversio:
Age float64
Marks int64
Fee int64
dtype: object
6. Convert entire dataframe int to float using apply()
In this example, we are converting the whole dataframe datatype integer to float by using apply() function. We are calling the apply() with the dataframe object.
import pandas as pd
import numpy as np
Student_dict = {
'Age': [2.5, 3.6, 3.7],
'Marks':[100.5,100.7, 10.78],
'Fee':[10.5,200.70, 105.78]
}
df = pd.DataFrame(Student_dict)
print('Existing datatype:\n',df.dtypes)
df = df.apply(np.int64)
print('\n',df,'\n')
print('\n datatype after conversio:\n',df.dtypes)
Output
Existing datatype:
Age float64
Marks float64
Fee float64
dtype: object
Age Marks Fee
0 2 100 10
1 3 100 200
2 3 10 105
datatype after conversio:
Age int64
Marks int64
Fee int64
dtype: object
Summary
In this post, we have learned how to Convert multiple float columns to int Pandas Dataframe, how to convert the entire dataframe column float to int using the astype() method of Python Pandas library.