pandas: Split String into Multiple Columns#
Package Import#
import pandas as pd
import numpy as np
Dataset Import#
The dataset used in this notebook is from Kaggle - Pokemon.
data = pd.read_csv('data/Pokemon.csv')
data
Split a string into multiple columns:#
This is extremely useful when we have Name
, and we want to split into First
,Middle
,Last
:
df = pd.DataFrame(dict(name=['John Arthur Doe', 'Jane Ann Smith'], location=['Los Angeles, CA', 'Washington, DC']))
df
df[['first','middle','last']] = df.name.str.split(expand=True) # if not specify delimiters, split based on whitespace
df