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 DataFrame Convert 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 CSV to Pandas Dataframe
In this article, we will discuss how to convert CSV to Pandas Dataframe, this operation can be performed using pandas.read_csv reads a comma-separated values (csv) file into DataFrame. Example 1: In the below program we are going to convert nba.csv into a data frame and then display it. Python Code
1 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
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 In
7 min read
How To Convert Pandas Column To List
One of the common tasks when working with a DataFrame in Pandas is converting a column to a list. In this article we will learn how to convert a Pandas column to a list using various methods. 1. Using tolist()One can convert a pandas column to a list using tolist() function which works on the Pandas
4 min read
Convert Data Frame Column to Numeric in R
R DataFrame is made up of three principal components, the data, rows, and columns. Data frames in R are versatile data structures to matrices where each column can contain different data types. This versatility allows for complex data analysis tasks. Converting columns to numeric type is a common op
3 min read
Convert the data type of Pandas column to int
In this article, we are going to see how to convert a Pandas column to int. Once a pandas.DataFrame is created using external data, systematically numeric columns are taken to as data type objects instead of int or float, creating numeric tasks not possible. We will pass any Python, Numpy, or Pandas
2 min read
How to scale Pandas DataFrame columns ?
When a dataset has values of different columns at drastically different scales, it gets tough to analyze the trends and patterns and comparison of the features or columns. So, in cases where all the columns have a significant difference in their scales, are needed to be modified in such a way that a
2 min read
Count Frequency of Columns in Pandas DataFrame
When working with data in Pandas counting how often each value appears in a column is one of the first steps to explore our dataset. This helps you understand distribution of data and identify patterns. Now weâll explore various ways to calculate frequency counts for a column in a Pandas DataFrame.
2 min read