Split all Data in a Column in Pandas (Python)

I'm trying to work with polling data from RealClearPolitics, where the polling date is given a date range, broken down like below.

1      11/1 - 11/7
2      11/4 - 11/6
3      11/4 - 11/7
4      11/4 - 11/7
5      11/1 - 11/7

I want to break these two date into separate columns for my EDA and have hit a bit of a roadblock.

I have had no problem splitting individual rows by using

import regex as re

re.split(pattern=' - ', string=polling_data['Date'][222])

but when I try to use it on the whole row in the Pandas Dataframe

for date in range(len(polling_data)):
    date_range = re.split(pattern=' - ', string=polling_data['Date'][date])

it keeps giving me errors.

I've tried rewording it and doing it without the for loop but keep getting errors ranging form 'does not work on series' to a full paragraph of errors that I just don't understand.

Anyone know how to make this work?

Upvotes: 0

Views: 50

Answers (1)

Pete Breslin
Pete Breslin

Reputation: 111

Please post a sample to test. This might work:

polling_data[['Date1','Date2']] = polling_data['Date'].str.split(' - ', expand=True)

Upvotes: 5

Related Questions