0

I need help with this pandas split with regex. I'm getting the error ValueError: Columns must be same length as key.

my column of data is like this

PURCHASE AUTHORIZED ON 03/30 UOFU BOOKSTORE 1 …
PURCHASE AUTHORIZED ON 03/29 WM SUPERC Wal-Mart Sup …
PURCHASE AUTHORIZED ON 03/29 KFC/AW #526 …
PURCHASE AUTHORIZED ON 03/31 UU VISITOR PARKING …
ATM WITHDRAWAL AUTHORIZED ON 04/03 Main Street …

my code is

df[['Auth_date', 'Description']] = df['Description'].str.split('(?<=\d{2}\d{2}).', regex=True)

desired results would be.

Auth_date                            Description
PURCHASE AUTHORIZED ON 03/30         UOFU BOOKSTORE 1 …
PURCHASE AUTHORIZED ON 03/29         WM SUPERC Wal-Mart Sup …
PURCHASE AUTHORIZED ON 03/29         KFC/AW #526 …
PURCHASE AUTHORIZED ON 03/31         UU VISITOR PARKING …
ATM WITHDRAWAL AUTHORIZED ON 04/03   Main Street …
7
  • Add expand=True Commented Jul 23, 2022 at 2:21
  • @BeRT2me Are you sure? I tried ...str.split('(?<=\d{2}\d{2}).', expand=True, regex=True). It didn't work. Commented Jul 23, 2022 at 2:23
  • Both yes and no. It looks like your Regex may be incorrect, which is a different problem. Commented Jul 23, 2022 at 2:25
  • re.split('(?<=\d{2}\d{2}).', "PURCHASE AUTHORIZED ON 03/30 UOFU BOOKSTORE") Doesn't split your string. Commented Jul 23, 2022 at 2:26
  • @BeRT2me No it does not. Commented Jul 23, 2022 at 2:31

1 Answer 1

1

Given:

                                         Description
0    PURCHASE AUTHORIZED ON 03/30 UOFU BOOKSTORE 1 …
1  PURCHASE AUTHORIZED ON 03/29 WM SUPERC Wal-Mar...
2         PURCHASE AUTHORIZED ON 03/29 KFC/AW #526 …
3  PURCHASE AUTHORIZED ON 03/31 UU VISITOR PARKING …
4   ATM WITHDRAWAL AUTHORIZED ON 04/03 Main Street …

Doing:

df[['Auth_date', 'Description']] = df['Description'].str.split('(?<=\d{2}/\d{2}).', expand=True, regex=True)
print(df)

Output:

                Description                           Auth_date
0        UOFU BOOKSTORE 1 …        PURCHASE AUTHORIZED ON 03/30
1  WM SUPERC Wal-Mart Sup …        PURCHASE AUTHORIZED ON 03/29
2             KFC/AW #526 …        PURCHASE AUTHORIZED ON 03/29
3      UU VISITOR PARKING …        PURCHASE AUTHORIZED ON 03/31
4             Main Street …  ATM WITHDRAWAL AUTHORIZED ON 04/03

Works fine for me.

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.