Type object to int python

10 tricks for converting Data to a Numeric Type in Pandas

Pandas tips and tricks to help you get started with Data Analysis

When doing data analysis, it is important to ensure correct data types. Otherwise, you may get unexpected results or errors. In the case of Pandas, it will correctly infer data types in many cases and you can move on with your analysis without any further thought on the topic.

Despite how well pandas works, at some point in your data analysis process you will likely need to explicitly convert data from one type to another. This article will discuss how to change data to a numeric type. More specifically, you will learn how to use the Pandas built-in methods astype() and to_numeric() to deal with the following common problems:

  1. Converting string/int to int/float
  2. Converting float to int
  3. Converting a column of mixed data types
  4. Handling missing values
  5. Converting a money column to float
  6. Converting boolean to 0/1
  7. Converting multiple data columns at once
  8. Defining data types when reading a CSV file
  9. Creating a custom function to convert data type
  10. astype() vs. to_numeric()

For demonstration, we create a dataset and will load it with a function:

import pandas as pd
import numpy as np
def load_df():
return pd.DataFrame( 'string_col': ['1','2','3','4'],
'int_col': [1,2,3,4],
'float_col': [1.1,1.2,1.3,4.7],
'mix_col': ['a', 2, 3, 4],
'missing_col': [1.0, 2, 3, np.nan],
'money_col': ['£1,000.00','£2,400.00','£2,400.00','£2,400.00'],
'boolean_col': [True, False, True, True],
'custom': ['Y', 'Y', 'N', 'N']
>)
df = load_df()

Please check out the Github repo for the source code.

Читайте также:  Установка сигнализации питон qx1

Источник

How to convert dtype ‘object’ to int in Pandas?

In this article, we will discuss multiple ways to convert any column with ‘object’ dtype to an integer in pandas.

Table of Content

Preparing dataset

To quickly get started, let’s create a sample dataframe to experiment. We’ll use the pandas library with some random data.

import pandas as pd # List of Tuples employees = [('Shubham', 'India', 'Tech', "5", 4), ('Riti', 'India', 'Design' , "7", 7), ('Shanky', 'India', 'PMO' , "2", 2), ('Shreya', 'India', 'Design' , "2", 0), ('Aadi', 'US', 'PMO', "11", 5), ('Sim', 'US', 'Tech', "4", 4)] # Create a DataFrame object from list of tuples df = pd.DataFrame(employees, columns=['Name', 'Location', 'Team', 'Experience', 'RelevantExperience'], index = ['A', 'B', 'C', 'D', 'E', 'F']) print(df)

Contents of the created dataframe are,

Name Location Team Experience RelevantExperience A Shubham India Tech 5 4 B Riti India Design 7 7 C Shanky India PMO 2 2 D Shreya India Design 2 0 E Aadi US PMO 11 5 F Sim US Tech 4 4

Also, let’s check the dtypes of the columns

Frequently Asked:

 Index: 6 entries, A to F Data columns (total 5 columns): # Column Non-Null Count Dtype --- ------ -------------- ----- 0 Name 6 non-null object 1 Location 6 non-null object 2 Team 6 non-null object 3 Experience 6 non-null object 4 RelevantExperience 6 non-null int64 dtypes: int64(1), object(4) memory usage: 288.0+ bytes

As observed, the column “Experience” is stored as “object” dtype. So, we will convert it to the int dtype using the methods below.

Approach 1: Using astype() function

This is the simplest method and property of any pandas Series to convert any dtype using the “astype()” function. Let’s understand by converting the column “Experience” to an integer.

# convert dtype of column to "int" df['Experience'] = df['Experience'].astype(str).astype(int) print(df['Experience'])
A 5 B 7 C 2 D 2 E 11 F 4 Name: Experience, dtype: int64

As observed, we have converted the dtype from “object” to “int” for the “Experience” column. We can save the output back in the “Experience” column for further use.

Approach 2: Using convert_dtypes() method

The convert_dtypes() method automatically understands the data type of any column based on the values stored and converts them to the suitable dtype. Let’s again try to convert the column “Experience” to integer dtype.

# convert dtype of columns df.convert_dtypes().info()
 Index: 6 entries, A to F Data columns (total 5 columns): # Column Non-Null Count Dtype --- ------ -------------- ----- 0 Name 6 non-null string 1 Location 6 non-null string 2 Team 6 non-null string 3 Experience 6 non-null Int64 4 RelevantExperience 6 non-null Int64 dtypes: Int64(2), string(3) memory usage: 300.0+ bytes

As observed, we passed the entire DataFrame and it converted all the first three columns as “string” and the “Experience” column as integers based on the type of values stored in each column.

Approach 3: Using pandas.to_numeric() function

Another way is to use pandas.to_numeric function to convert any column into numeric dtype. Let’s experiment with the “Experience” column again.

# convert dtype of column to numeric df['Experience'] = pd.to_numeric(df['Experience'], errors='coerce') print(df['Experience'])
A 5 B 7 C 2 D 2 E 11 F 4 Name: Experience, dtype: int64

We have similar output as the first method. Here, the errors=”coerce” attribute means that in case of any errors (for example – converting “4.0” into int is not possible), it will return NaN instead of throwing an error.

Summary

In this article, we have discussed how to convert dtype ‘object’ to int in Pandas. Thanks.

Share your love

Leave a Comment Cancel Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Terms of Use

Disclaimer

Copyright © 2023 thisPointer

To provide the best experiences, we and our partners use technologies like cookies to store and/or access device information. Consenting to these technologies will allow us and our partners to process personal data such as browsing behavior or unique IDs on this site and show (non-) personalized ads. Not consenting or withdrawing consent, may adversely affect certain features and functions.

Click below to consent to the above or make granular choices. Your choices will be applied to this site only. You can change your settings at any time, including withdrawing your consent, by using the toggles on the Cookie Policy, or by clicking on the manage consent button at the bottom of the screen.

The technical storage or access is strictly necessary for the legitimate purpose of enabling the use of a specific service explicitly requested by the subscriber or user, or for the sole purpose of carrying out the transmission of a communication over an electronic communications network.

The technical storage or access is necessary for the legitimate purpose of storing preferences that are not requested by the subscriber or user.

The technical storage or access that is used exclusively for statistical purposes. The technical storage or access that is used exclusively for anonymous statistical purposes. Without a subpoena, voluntary compliance on the part of your Internet Service Provider, or additional records from a third party, information stored or retrieved for this purpose alone cannot usually be used to identify you.

The technical storage or access is required to create user profiles to send advertising, or to track the user on a website or across several websites for similar marketing purposes.

Источник

Оцените статью