Change the data type of given a column using Pandas

In this python pandas program, we will change the data type of given a column using pandas library.

Steps to solve the program
  1. Import pandas library as pd.
  2. Create a series using pd.Series().
  3. Change the data-type of the series from object to float using pd.to_numeric(series name, errors=’coerce’).
  4. It will convert all the non-string elements to float and if there is a string element it will show NaN instead.
  5. Print the output
				
					import pandas as pd
df1 = pd.Series([54,27.90,'sqa',33.33,'tools'])
print("Original Data Series:")
print(df1)
print("Change the said data type to numeric:")
df2 = pd.to_numeric(df1, errors='coerce')
print(df2)
				
			

Output :

				
					Original Data Series:
0       54
1     27.9
2      sqa
3    33.33
4    tools
dtype: object
Change the said data type to numeric:
0    54.00
1    27.90
2      NaN
3    33.33
4      NaN
dtype: float64
				
			

convert a NumPy array to a Pandas series

convert a given Series to an array

Leave a Comment