Encode and decode a column of a dataframe in python – pandas

In this section we will learn how to encode and decode a column of a dataframe in python pandas.

We will see an example to encode a column of a dataframe in python pandas and another example to decode the encoded column.

Encode a column of dataframe in python:

Create dataframe:

#create dataframe

import pandas as pd
d = {'Quarters' : ['quarter1','quarter2','quarter3','quarter4'],
'Revenue':[23400344.567,54363744.678,56789117.456,4132454.987]}
df=pd.DataFrame(d)
print df

Resultant dataframe will be

Encode and decode a column in a dataframe in python 1

 

Encode a column Example:

Lets encode the column named Quarters and save it in the column named Quarters_encoded.

# Encode Quarters dataframe in Python

df['Quarters_encoded'] = map(lambda x: x.encode('base64','strict'), df['Quarters'])
print df

encode() function with  codec ‘base64’ and error handling scheme  ‘strict’ is used along with the map() function to encode a column of a dataframe and it is stored in the column named quarter_encoded as shown above so the resultant dataframe will be

Encode and decode a column in a dataframe in python 2

 

Decode a column of dataframe in python:

We will be using the already encoded value as input to the decode function

# Decode dataframe in Python

df['Quarters_decoded'] = map(lambda x: x.decode('base64','strict'), df['Quarters_encoded'])
print df

Note: you should use the same encoding and error parameters (‘base64’ and ‘strict’) to decode the string.

So the output will be

Encode and decode a column in a dataframe in python 3

 

previous Encode and decode a column in a dataframe in python                                                                                                           next Encode and decode a column in a dataframe in python

Author

  • Sridhar Venkatachalam

    With close to 10 years on Experience in data science and machine learning Have extensively worked on programming languages like R, Python (Pandas), SAS, Pyspark.

    View all posts