Convert Pandas Dataframe Column To Float
Last Updated :
11 Jul, 2024
Converting columns to floats in Pandas DataFrame is a very crucial step for data analysis. Converting columns to float values can help you perform various arithmetic operations and plot graphs.
In this article, we’ll look at different ways to convert a column to a float in DataFrame.
- Using DataFrame.astype()
- Using pandas.to_numeric()
- Handling Non-numeric Values and Missing Values
- Converting Multiple Columns
- Convert the entire data frame
Convert Pandas Dataframe Column To Float DataType
Importing Pandas
Python
Creating a sample dataframe
Python
# Sample DataFrame
data = {'IntegerColumn': [10, 20, 30],
'StringColumn': ['15.3', '25.6', '35.8']}
df = pd.DataFrame(data)
Method 1: Using DataFrame.astype()
DataFrame.astype() method is used to cast a Pandas object to a specified dtype. astype() function is used to convert a particular column data type to another data type.
Here, we created a sample data frame with two columns containing integers and strings and then we converted the string column to a float column using the astype() function.
Python
# Print dtypes before conversion
print("Data types before conversion:")
print(df.dtypes)
# Convert 'StringColumn' to float using astype()
df['StringColumn'] = df['StringColumn'].astype(float)
# Print dtypes after conversion
print("\nData types after conversion:")
print(df.dtypes)
# Print the DataFrame
print("\nDataFrame after conversion:")
print(df)
Output:
Data types before conversion:
IntegerColumn int64
StringColumn object
dtype: object
Data types after conversion:
IntegerColumn int64
StringColumn float64
dtype: object
DataFrame after conversion:
IntegerColumn StringColumn
0 10 15.3
1 20 25.6
2 30 35.8
As you can observe the datatype of the string column is changed to float after using astype() function.
Method 2: Using pandas.to_numeric()
pandas.to_numeric() is a function in Pandas which is used to convert argument to a numeric type. Here, we are converting the string column to float using to_numeric() function.
We are printing the data type of the columns before and after the conversion of column to understand the conversion.
Python
# Print dtypes before conversion
print("Data types before conversion:")
print(df.dtypes)
# Convert 'StringColumn' to float using to_numeric()
df['StringColumn'] = pd.to_numeric(df['StringColumn'])
# Print dtypes after conversion
print("\nData types after conversion:")
print(df.dtypes)
# Print the DataFrame
print("\nDataFrame after conversion:")
print(df)
Output:
Data types before conversion:
IntegerColumn int64
StringColumn object
dtype: object
Data types after conversion:
IntegerColumn int64
StringColumn float64
dtype: object
DataFrame after conversion:
IntegerColumn StringColumn
0 10 15.3
1 20 25.6
As you observe the output the data type of the string column is changed from object to float after using to_numeric() function.
Handling Non-numeric Values or Missing Values while coverting the DataType to Float
We can handle Non-convertible values, Missing values, and NaN values by using errors='coerce' parameter in pandas.to_numeric() function, errors='coerce' parameter instructs Pandas to replace non-convertible values with NaN (Not a Number).
Here, we created a dataframe with missing values and alphabets (can't convert) and applying pandas.to_numeric() function with errors='coerce' parameter. This output the dataframe with the NaN values where the values are not convertible.
Python
# Sample DataFrame with non-numeric and missing values
data = {'Column1': ['10.5', '20.7', '30.2', 'xyz'],
'Column2': ['15.3', '25.6', '35.8', '']}
df = pd.DataFrame(data)
print('Original DataFrame')
print(df)
# Convert columns to float, handling errors and missing values
df['Column1'] = pd.to_numeric(df['Column1'], errors='coerce')
df['Column2'] = pd.to_numeric(df['Column2'], errors='coerce')
# Print dtypes after conversion
print("\nData types after conversion:")
print(df.dtypes)
# Print the DataFrame
print("\nDataFrame after conversion:")
print(df)
Output:
Original DataFrame
Column1 Column2
0 10.5 15.3
1 20.7 25.6
2 30.2 35.8
3 xyz
Data types after conversion:
Column1 float64
Column2 float64
dtype: object
DataFrame after conversion:
Column1 Column2
0 10.5 15.3
1 20.7 25.6
2 30.2 35.8
3 NaN NaN
Converting Multiple Columns
We can convert multiple columns to float in the dataframe by passing multiple columns while conversion. Here is a simple syntax,
df[['C1', 'C2']] = df[['C1', 'C2']].astype(float)
C1, C2 are the columns of the dataframe to be converted.
Now, we created a dataframe with two columns as strings and converted those two columns to float using the above syntax.
Python
# Sample DataFrame
data = {'C1': ['10.5', '20.7', '30.2'],
'C2': ['15.3', '25.6', '35.8']}
df = pd.DataFrame(data)
# Print dtypes before conversion
print("Data types before conversion:")
print(df.dtypes)
# Convert multiple columns to float using astype()
df[['C1', 'C2']] = df[['C1', 'C2']].astype(float)
# Print dtypes after conversion
print("\nData types after conversion:")
print(df.dtypes)
# Print the DataFrame
print("\nDataFrame after conversion:")
print(df)
Output:
Data types before conversion:
C1 object
C2 object
dtype: object
Data types after conversion:
C1 float64
C2 float64
dtype: object
DataFrame after conversion:
C1 C2
0 10.5 15.3
1 20.7 25.6
2 30.2 35.8
Convert the entire DataFrame
We can convert the entire DataFrame using astype() function and passing float as datatype.
Python
# Sample DataFrame
data = {'C1': ['10.5', '20.7', '30.2'],
'C2': ['15.3', '25.6', '35.8']}
df = pd.DataFrame(data)
# Print dtypes before conversion
print("Data types before conversion:")
print(df.dtypes)
# Convert the entire DataFrame to float
df = df.astype(float)
# Print dtypes after conversion
print("\nData types after conversion:")
print(df.dtypes)
# Print the DataFrame
print("\nDataFrame after conversion:")
print(df)
int("GFG")
Output:
Data types before conversion:
C1 object
C2 object
dtype: object
Data types after conversion:
C1 float64
C2 float64
dtype: object
DataFrame after conversion:
C1 C2
0 10.5 15.3
1 20.7 25.6
2 30.2 35.8
Conclusion:
In conclusion, converting columns to float in a Pandas DataFrame is used for mathematical operations and data analysis. In this article,we discussed about methods like DataFrame.astype() and pandas.to_numeric() and their usage in converting columns to float and handling missing values.
Similar Reads
Convert a Dataframe Column to Integer in Pandas Converting DataFrame columns to the correct data type is important especially when numeric values are mistakenly stored as strings. Let's learn how to efficiently convert a column to an integer in a Pandas DataFrameConvert DataFrame Column to Integer - using astype() Methodastype() method is simple
3 min read
How to Convert Index to Column in Pandas Dataframe? Pandas is a powerful tool which is used for data analysis and is built on top of the python library. The Pandas library enables users to create and manipulate dataframes (Tables of data) and time series effectively and efficiently. These dataframes can be used for training and testing machine learni
2 min read
Convert Bytes To a Pandas Dataframe In Python, bytes are a built-in data type used to represent a sequence of bytes. They are immutable sequences of integers, with each integer typically representing a byte of data ranging from 0 to 255. Convert Bytes Data into a Python Pandas Dataframe?We can convert bytes into data frames using diff
4 min read
How to Convert Pandas DataFrame into a List? In this article, we will explore the process of converting a Pandas DataFrame into a List, We'll delve into the methods and techniques involved in this conversion, shedding light on the versatility and capabilities of Pandas for handling data structures in Python.Ways to convert Pandas DataFrame Int
7 min read
Convert Boolean To String In Pandas Dataframe Pandas, a powerful data manipulation library in Python, offers multiple ways to convert boolean values to strings within a DataFrame. In this article, we will see how to convert boolean to String in Pandas DataFrame in Python. Python Convert Boolean To String In Pandas DataframeBelow are some exampl
3 min read
Convert Data Frame Column to Numeric in R In R, converting DataFrame columns to numeric is a common and important step in data preprocessing, particularly for statistical modeling and analysis. Below are effective methods to perform this conversion using base R and the readr package.Method 1: Convert One Column to NumericStart by checking t
2 min read