If you work with a Pandas database that contains variables of different data types, you can convert a certain character/line variable/category into a numeric variable. One of the applications of this transformation is that it allows rapid analysis of correlations.

In this article, we will see some examples of converting a character variable to an entire variable in Pandas. For example, we will transform a character variable with three different values, namely Adelie, Donkey Bridge and Chinband, to 0/1/2. Note that this differs from converting integer values stored as character variables, such as 1, 2 and 3, to integers 1/2/3. For this type of conversion we can use the Pandas methods as_numeric() or astype(int).

How do you encode a character variable as an integer using pands? How to encode a variable as a whole number using pands?
Let’s load up the parcels needed for mapping.

imports of pandas as pd
imports of sea pandas as sns

We will use the Palmer Penguin dataset as a variable in the Seaborn integrated datasets.

penguins = sns.load_dataset(penguins)
penguins = penguins.dropna()

You can see that character variables have data types that are called standard objects in Pandas.

Penguins.

see object
island object
bill_length_mm float64
bill_depth_mm float64
flipper_length_mm float64
body_mass_g float64
sex object
dtype: object

1. Coding character Variable for integrators with pandas of theseries

A solution to convert a character variable into integer values is to work with a set of variables. We can obtain the variable of interest as a series by using

Penguins.

0 Adeli
1 Adeli
2 Adeli
4 Adeli
5 Adeli

338 Gentoo
340 Gentoo
341 Gentoo
342 Gentoo
343 Gentoo
Title : View, length: 333, type : Subject

Next, you have to convert the character variable into a categorical variable using the axis type Panda().

Penguins.species.astype(category)

0 Adeli
1 Adeli
2 Adeli
4 Adeli
5 Adeli

338 Gentoo
340 Gentoo
341 Gentoo
342 Gentoo
343 Gentoo
Title : View, length: 333, type : Category
Categories (3, Object) : (Adelie, Jugular, Gentoo)

Then determine the integers using the code cat.codes for the categorical variable.

Penguin species.astip(category).cat.codes

0 0
1 0
2 0
4 0
5 0

338 2
340 2
341 2
342 2
343 2
Length: 333, type: int8

To store the transformed variable in the original data frame, we can rename it to

penguins.species = penguins.species.astip(category).cat.

And now our updated data block looks like this.

penguins.head()
species island cap_length_mm mouth_depth_mm flipper_length_mm body_massa_g genus
0 0 Torgersen 39,1 18,7 181,0 3750,0 Male
1 0 Torgersen 39.5 17,4 186,0 3800,0 Women
2 0 Torgersen 40,3 18,0 195,0 3250,0 Men
4 0 Torgersen 36,7 19,3 193,0 3450,0 Women
5 0 Torgersen 39,3 20,6 190,0 3650,0 Men

2. Character variable coding for ramps using the Pandas data frame

Another way to encode a character variable into an entire variable is by working with the variable as a data frame object. We can define the Panda data subframe as follows.

Penguins [[Species]]

Species
0 Adeli
1 Adeli
2 Adeli
4 Adeli
5 Adeli
… …
338 Gentoo
340 Gentoo
341 Gentoo
342 Gentoo
343 Gentoo
333 rows × 1 column

Then use apply() to convert each element to an integer, as shown below.

penguins[[species]].apply(lambda col:pd.Categorical(col).codes)

Types
0 0
1 0
2 0
4 0
5 0
… ….
338 2
340 2
341 2
342 2
343 2
333 Lines × 1 column

To store the transformed variable as a variable in the data frame, we use

Penguins[[species]] =Penguins[[species]].apply(lambda col:pd.Categorical(col).codes)

pandas convert string column to int,pandas convert string to int,pandas change column type to int,pandas convert string column to float,pandas to int,pandas convert float to int,convert string to dataframe python,astype pandas,pandas map string to int,pandas to_numeric,cannot convert float nan to integer

You May Also Like

How to Secure your Privacy in Windows 10 using Built-In Rules

Hey there! Before reading the article which is about How to Secure…

How to enable Reader View in Chrome on Mac [2 Best Ways]

If you have already used Safari or Firefox on the Mac, you…

Root Sony Xperia V LT25i (mint) and Install TWRP Recovery 3.5.0

If you own a Sony Xperia V LT25i (codename: tsubasa) and want…

Open Multiple Programs with One Shortcut in Windows 10

Many people have trouble with Windows 10, and that is because it…