« Pandas
Cast dtype to a particular dtype with options.
Let us change one type to other. Here column MATH is changed to string and '2' ( '2' dtpye is string not integer ) is added.
import pandas as pd
my_dict={'NAME':['Ravi','Raju','Alex','Ron','King','Jack'],
'ID':[1,2,3,4,5,6],
'MATH':[80,40,70,70,70,30],
'ENGLISH':[80,70,40,50,60,30]}
my_data = pd.DataFrame(data=my_dict)
print(my_data['MATH'].astype(str)+'2')
Output
0 802
1 402
2 702
3 702
4 702
5 302
Let us change the last line only in above code.
print(my_data['MATH']+2)
Output
0 82
1 42
2 72
3 72
4 72
5 32
Name: MATH, dtype: int64
Let us change the MATH column to int32
print(my_data['MATH'].astype('int32'))
Output
0 80
1 40
2 70
3 70
4 70
5 30
Name: MATH, dtype: int32
We can use dtypes
to get the data type of columns.
import pandas as pd
my_dict={'NAME':['Ravi','Raju','Alex','Ron','King','Jack'],
'ID':[1,2,3,4,5,6],
'MATH':[80,40,70,70,70,30],
'ENGLISH':[80,70,40,50,60,30]}
my_data = pd.DataFrame(data=my_dict)
print(my_data.dtypes) # dtype of all columns
print('----')
print(my_data['MATH'].dtypes) # dtype of MATH column
print(my_data['NAME'].dtypes) # dtype of NAME column
Output
NAME object
ID int64
MATH int64
ENGLISH int64
dtype: object
----
int64
object
We can successfully convert the data types if data matches to new data type. Otherwise we have to clean the data before using astype()
Data Cleaning
« Pandas
to_timedelta()
dtypes()
select_dtypes()
timedelta64()
← Subscribe to our YouTube Channel here