String Split in column of dataframe in pandas python

String Split in column of dataframe in pandas python can be done by using str.split() function. Let’s see how to

  • Split the string of the column in pandas python with examples

First let’s create a dataframe


import pandas as pd
import numpy as np

df1 = {
    'State':['Arizona AZ','Georgia GG','Newyork NY','Indiana IN','Florida FL']}

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

df1 will be

String Split in column of dataframe in pandas python 1

 

String split the column of dataframe in pandas python:

String split can be achieved in two steps

(i)            Convert the dataframe column to list and split the list

(ii)            Convert the splitted list into dataframe.

 

Step 1: Convert the dataframe column to list and split the list:

df1.State.str.split().tolist()

so resultant splitted list will be

String Split in column of dataframe in pandas python 2

 

Step 2: Convert the splitted list into new dataframe:


df2 = pd.DataFrame(df1.State.str.split().tolist(), columns="State State_code".split())
print(df2)

splitted list is converted into dataframe with 2 columns. Column name of the dataframe also being set(State ,State_code) as shown.

So the resultant dataframe will be

String Split in column of dataframe in pandas python 3

 

p String Split in column of dataframe in pandas python                                                                                                                n String Split in column of dataframe in pandas python