Reverse the string of column in pandas python

Reverse the string of column in pandas python can be done in a roundabout using apply function. Let’s see how to

  •         Reverse the  string of column in pandas dataframe – python

With examples

First let’s create a dataframe

import pandas as pd
import numpy as np

#Create a DataFrame
df1 = {
    'State':['Arizona AZ','Georgia GG','Newyork NY','Indiana IN','Florida FL'],
   'Score':[62,47,55,74,31]}

df1 = pd.DataFrame(df1,columns=['State','Score'])
print(df1)

df1 will be

Reverse the string of column in pandas python 1

 

Reverse the String of the column in pandas

x[::-1]  is used to reverse the string of the column in pandas along with the apply function as shown below.

df1['State_reverse'] = df1.loc[:,'State'].apply(lambda x: x[::-1])
print(df1)

so the resultant dataframe will be

Reverse the string of column in pandas python 2

p Reverse the string of column in pandas python                                                                                                          n Reverse the string of column in pandas 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