0

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?

3
  • Could you provide a bit of sample data Commented Feb 26, 2020 at 20:10
  • We cannot diagnose your problem without your error messages. It would be better still to be able to reproduce it with sample data and methods. Commented Feb 26, 2020 at 20:11
  • I'm still pretty new to this, but I added the sample of date ranges as best I could. Commented Feb 26, 2020 at 20:13

1 Answer 1

5

Please post a sample to test. This might work:

polling_data[['Date1','Date2']] = polling_data['Date'].str.split(' - ', expand=True)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.