-2

I need to swap the list of names which is in the format of FirstName and LastName which in a dataframe one column, using python.

Below is the sample format: ~Adam Smith

The above need to change into ~Smith Adam

Is there any single line function available in python?

Could anyone help on this!!

1 Answers1

1

Using apply

import pandas as pd
df = pd.DataFrame({"names": ["Adam Smith", "Greg Rogers"]})
df["names"] = df["names"].apply(lambda x: " ".join(reversed(x.split()))) 
print(df)

Output:

         names
0   Smith Adam
1  Rogers Greg
Rakesh
  • 81,458
  • 17
  • 76
  • 113