Splitting a dataframe in pandas means making a single column into two columns. This is the answer to how to split a column in pandas.

A pandas dataframe column can be split by using the str.split() method in pandas.

import pandas as pd

df = pd.DataFrame({'Name': ['Steve Smith', 'Joe Nadal', 'Roger Federer'], 'Age':[32, 34, 36]})

# Results in a pandas DataFrame containing the splitted column

df[['First','Last']] = df.Name.str.split(" ",expand=True)  
How To Split A Column In Pandas - The Click Reader

The ” ” in str.split can be changed to anything to match the delimiter such as,

import pandas as pd

df = pd.DataFrame({'Name': ['Steve-Smith', 'Joe-Nadal', 'Roger-Federer'], 'Age':[32, 34, 36]})

# Results in a pandas DataFrame containing the splitted column

df[['First','Last']] = df.Name.str.split("-",expand=True)  

Hope this article answers your question about ‘How To Split A Column In Pandas?’

LEAVE A REPLY

Please enter your comment!
Please enter your name here