Pandas - Split One Column into Two Columns

Published: 20 August 2024
on channel: Learn Pandas
31
1

This is how I was able to split one column into two new columns.

import libraries
import pandas as pd
import numpy as np

set the seed
np.random.seed(0)

generate bus routes dataset
bus_routes = pd.DataFrame({
'route_id': np.arange(1, 101),
'route_name': ['Route ' + str(i) for i in np.arange(1, 101)],
'schedules': ['08:00-18:00' if i % 2 == 0 else '09:00-20:00' for i in np.arange(1, 101)]
})

create two new columns
bus_routes[['start_time','end_time']] = bus_routes['schedules'].str.split('-', expand=True)

🔥 Free Pandas Course: https://hedaro.gumroad.com/l/tqqfq


Watch video Pandas - Split One Column into Two Columns online without registration, duration hours minute second in high quality. This video was added by user Learn Pandas 20 August 2024, don't forget to share it with your friends and acquaintances, it has been viewed on our site 31 once and liked it 1 people.