- Pandas Convert Column to Float in DataFrame
- 1. Quick Examples of Convert String to Float in DataFrame
- 2. pandas Convert String to Float
- 3. Convert Multiple Columns to Float
- 4. Convert All Columns to Float Type
- 5. Using pandas.to_numeric()
- 6. Handling Non-numeric Values
- 7. Replace the ‘NaN’ Values with Zeros
- 8. Replace Empty String before Convert
- Conclusion
- Related Articles
- References
- You may also like reading:
- Naveen (NNK)
- 7 ways to convert pandas DataFrame column to float
- Different methods to convert column to float in pandas DataFrame
- Create pandas DataFrame with example data
- Method 1 : Convert integer type column to float using astype() method
- Method 2 : Convert integer type column to float using astype() method with dictionary
- Method 3 : Convert integer type column to float using astype() method by specifying data types
- Method 4 : Convert string/object type column to float using astype() method
- Method 5 : Convert string/object type column to float using astype() method with dictionary
- Method 6 : Convert string/object type column to float using astype() method by specifying data types
- Method 7 : Convert to float using convert_dtypes()
- Summary
- Reference
Pandas Convert Column to Float in DataFrame
By using pandas DataFrame.astype() and pandas.to_numeric() methods you can convert a column from string/int type to float. In this article, I will explain how to convert one or multiple string columns to float type using examples.
1. Quick Examples of Convert String to Float in DataFrame
If you are in a hurry, below are some quick examples of how to convert string to float. You can apply these to convert from any type in Pandas
) # Convert all columns to floats df = df.astype(float) print(df.dtypes) # Convert numeric function string to float df['Discount'] = pd.to_numeric(df['Discount']) print(df.dtypes) # Convert DataFrame column from string to float df["Discount"] = pd.to_numeric(df["Discount"], downcast="float") print(df.dtypes) # Convert each value of the column to a string df['Discount'] = pd.to_numeric(df['Discount'], errors='coerce') print(df.dtypes) # Using df.replace() to replace nan values 0 before convertion df['Discount'] = pd.to_numeric(df['Discount'], errors='coerce') df = df.replace(np.nan, 0, regex=True) print(df) print(df.dtypes) # Replace empty string ('') with np.nan before convertion df['Discount']=df.Discount.replace('',np.nan).astype(float) print(df) print(df.dtypes)
Now, let’s create a DataFrame with a few rows and columns, execute the above examples and validate the results. Our DataFrame contains column names Fee and Discount .
) df = pd.DataFrame(technologies) print(df.dtypes)
You can identify the data type of each column by using dtypes . For Instance, print(df.dtypes) outputs as below. Here object means a String type.
2. pandas Convert String to Float
Use pandas DataFrame.astype() function to convert column from string/int to float, you can apply this on a specific column or on an entire DataFrame. To cast the data type to 54-bit signed float, you can use numpy.float64 , numpy.float_ , float , float64 as param. To cast to 32-bit signed float, use numpy.float32 or float32 .
The Below example converts Fee column from string dtype to float64 .
You can also use Series.astype() to convert a specific column. since each column on DataFrame is pandas Series, I will get the column from DataFrame as Series and use astype() function. In the below example df.Fee or df[‘Fee’] returns Series object.
Yields same output as above.
3. Convert Multiple Columns to Float
You can also convert multiple columns to float by sending dict of column name -> data type to astype() method. The below example converts column Fee and Discount to float dtype.
4. Convert All Columns to Float Type
By default astype() function converts all columns to the same type. The below example converts all DataFrame columns to float type. If you have any column with alpha-numeric values, you will get an error.
5. Using pandas.to_numeric()
Alternatively, you can convert all string columns to float type using pandas.to_numeric() . For example use df[‘Discount’] = pd.to_numeric(df[‘Discount’]) function to convert ‘Discount’ column to float.
6. Handling Non-numeric Values
When you have some cells with character values on a column you wanted to convert to float, it returns an error. To ignore the error and convert the char values to NaN use errors=’coerce’ attribute.
This yields the same output as above.
7. Replace the ‘NaN’ Values with Zeros
Use df=df.replace(np.nan,0,regex=True) function to replace the ‘NaN’ values with ‘0’ values.
8. Replace Empty String before Convert
If you have empty values in a string, convert empty string (») with np.nan before converting it to float.
) df = pd.DataFrame(technologies) # Replace empty string ('') with np.nan df['Discount']=df.Discount.replace('',np.nan).astype(float) print(df) print(df.dtypes)
Conclusion
In this article, you have learned how to convert single, multiple, and all columns from string type to float in Pandas DataFrame using DataFrame.astype(float) and pandas.to_numeric() function.
Related Articles
References
You may also like reading:
Naveen (NNK)
SparkByExamples.com is a Big Data and Spark examples community page, all examples are simple and easy to understand and well tested in our development environment Read more ..
7 ways to convert pandas DataFrame column to float
Different methods to convert column to float in pandas DataFrame
In this tutorial we will discuss how to convert DataFrame columns into float using the following methods:
Convert integer type column to float:
- Using astype() method
- Using astype() method with dictionary
- Using astype() method by specifying data types
Convert string/object type column to float:
- Using astype() method
- Using astype() method with dictionary
- Using astype() method by specifying data types
Convert to float using convert_dtypes()
Create pandas DataFrame with example data
DataFrame is a data structure used to store the data in two dimensional format. It is similar to table that stores the data in rows and columns. Rows represents the records/ tuples and columns refers to the attributes.
We can create the DataFrame by using pandas.DataFrame() method.
pandas.DataFrame(input_data,columns,index)
It will take mainly three parameters
- input_data is represents a list of data
- columns represent the columns names for the data
- index represent the row numbers/values
We can also create a DataFrame using dictionary by skipping columns and indices.
Example: Python Program to create a dataframe for market data from a dictionary of food items by specifying the column names.
#import the module import pandas #consider the food data food_input= <'id':['foo-23','foo-13','foo-02','foo-31'], 'name':['ground-nut oil','almonds','flour','cereals'], 'cost':[567,562,67,76], 'quantity':['1','2','3','2']>#pass this food to the dataframe by specifying rows dataframe=pandas.DataFrame(food_input,index = ['item-1', 'item-2', 'item-3', 'item-4']) #display the dataframe data types print(dataframe.dtypes)
id object name object cost int64 quantity object dtype: object
Method 1 : Convert integer type column to float using astype() method
Here we are going to convert the integer type column in DataFrame to integer type using astype() method. we just need to pass float keyword inside this method.
dataframe['column'].astype(float)
- dataframe is the input dataframe
- column is the integer type column to be converted to float
Python program to convert cost column to float
#import the module import pandas #consider the food data food_input= <'id':['foo-23','foo-13','foo-02','foo-31'], 'name':['ground-nut oil','almonds','flour','cereals'], 'cost':[567,562,67,76], 'quantity':['1','2','3','2']>#pass this food to the dataframe by specifying rows dataframe=pandas.DataFrame(food_input,index = ['item-1', 'item-2', 'item-3', 'item-4']) #convert the cost column data type (integer) into float dataframe['cost'] = dataframe['cost'].astype(float) #display data types print(dataframe.dtypes)
id object name object cost float64 quantity object dtype: object
Method 2 : Convert integer type column to float using astype() method with dictionary
Here we are going to convert the integer type column in DataFrame to float type using astype() method. we just need to pass float keyword inside this method through dictionary .
- dataframe is the input dataframe
- column is the integer type column to be converted to float
Example: Python program to convert cost column to float
#import the module import pandas #consider the food data food_input= <'id':['foo-23','foo-13','foo-02','foo-31'], 'name':['ground-nut oil','almonds','flour','cereals'], 'cost':[567.00,562.56,67.00,76.09], 'quantity':['1','2','3','2']>#pass this food to the dataframe by specifying rows dataframe=pandas.DataFrame(food_input,index = ['item-1', 'item-2', 'item-3', 'item-4']) #convert the cost column data type (integer) into float dataframe = dataframe.astype() #display data types print(dataframe.dtypes)
id object name object cost float64 quantity object dtype: object
Method 3 : Convert integer type column to float using astype() method by specifying data types
Here we are going to use astype() method twice by specifying types. first method takes the old data type i.e int and second method take new data type i.e float type
dataframe['column'].astype(int).astype(float)
Example: Python program to convert cost column to float
#import the module import pandas #consider the food data food_input= <'id':['foo-23','foo-13','foo-02','foo-31'], 'name':['ground-nut oil','almonds','flour','cereals'], 'cost':[567,562,67,76], 'quantity':['1','2','3','2']>#pass this food to the dataframe by specifying rows dataframe=pandas.DataFrame(food_input,index = ['item-1', 'item-2', 'item-3', 'item-4']) #convert the cost column data type (integer) into float dataframe['cost'] = dataframe['cost'].astype(int).astype(float) #display data types print(dataframe.dtypes)
id object name object cost float64 quantity object dtype: object
Method 4 : Convert string/object type column to float using astype() method
Here we are going to convert the string type column in DataFrame to float type using astype() method. we just need to pass float keyword inside this method.
dataframe['column'].astype(float)
- dataframe is the input dataframe
- column is the string type column to be converted to float
Example: Python program to convert quantity column to float
#import the module import pandas #consider the food data food_input= <'id':['foo-23','foo-13','foo-02','foo-31'], 'name':['ground-nut oil','almonds','flour','cereals'], 'cost':[567,562,67,76], 'quantity':['1','2','3','2']>#pass this food to the dataframe by specifying rows dataframe=pandas.DataFrame(food_input,index = ['item-1', 'item-2', 'item-3', 'item-4']) #convert the quantity column data type (string) into float dataframe['quantity'] = dataframe['quantity'].astype(float) #display data types print(dataframe.dtypes)
id object name object cost int64 quantity float64 dtype: object
Method 5 : Convert string/object type column to float using astype() method with dictionary
Here we are going to convert the string type column in DataFrame to float type using astype() method. we just need to pass float keyword inside this method through dictionary.
- dataframe is the input dataframe
- column is the string type column to be converted to float
Example: Python program to convert quantity column to float
#import the module import pandas #consider the food data food_input= <'id':['foo-23','foo-13','foo-02','foo-31'], 'name':['ground-nut oil','almonds','flour','cereals'], 'cost':[567,562,67,76], 'quantity':['1','2','3','2']>#pass this food to the dataframe by specifying rows dataframe=pandas.DataFrame(food_input,index = ['item-1', 'item-2', 'item-3', 'item-4']) #convert the quantity column data type (string) into integer dataframe = dataframe.astype() #display data types print(dataframe.dtypes)
id object name object cost int64 quantity float64 dtype: object
Method 6 : Convert string/object type column to float using astype() method by specifying data types
Here we are going to use astype() method twice by specifying types. first method takes the old data type i.e string and second method take new data type i.e float type
dataframe['column'].astype(str).astype(float)
Example: Python program to convert quantity column to float
#import the module import pandas #consider the food data food_input= <'id':['foo-23','foo-13','foo-02','foo-31'], 'name':['ground-nut oil','almonds','flour','cereals'], 'cost':[567,562,67,76], 'quantity':['1','2','3','2']>#pass this food to the dataframe by specifying rows dataframe=pandas.DataFrame(food_input,index = ['item-1', 'item-2', 'item-3', 'item-4']) #convert the quantity column data type (string) into float dataframe['quantity'] = dataframe['quantity'].astype(str).astype(float) #display data types print(dataframe.dtypes)
id object name object cost int64 quantity float64 dtype: object
Method 7 : Convert to float using convert_dtypes()
Here we are going to use convert_dtypes() method. It will automatically convert into float type.
Example: Python program to convert dataframe columns to float
#import the module import pandas #consider the food data food_input= <'id':['foo-23','foo-13','foo-02','foo-31'], 'name':['ground-nut oil','almonds','flour','cereals'], 'cost':[567.00,562.56,67.00,76.09], 'quantity':['1','2','3','2']>#pass this food to the dataframe by specifying rows dataframe=pandas.DataFrame(food_input,index = ['item-1', 'item-2', 'item-3', 'item-4']) #convert into int type dataframe = dataframe.convert_dtypes() print(dataframe.dtypes)
id string name string cost int64 quantity string dtype: object dtype: object
Summary
In this tutorial we discussed how to convert dataframe column to float type using astype() method through 7 scenarios by considering integer and string/object (str) types. Here Dictionary is involved in two methods to convert the data type.
Reference
Didn’t find what you were looking for? Perform a quick search across GoLinuxCloud
If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.
For any other feedbacks or questions you can either use the comments section or contact me form.
Thank You for your support!!