user10083444
user10083444

Reputation: 105

Data frame multiple columns splitting

I have a pandas dataframe of the following format.

0     06-10-2000 Deposit $40.00
1  09-12-2002 Withdraw $1000.00
2     27-06-2001 Deposit $47.00
3   07-12-2021 Withdraw $100.00
4    06-10-2022 Deposit $120.00
5     06-10-2000 Deposit $40.00
6    09-12-2024 Withdraw $50.00

How do I split each row in to 3 columns. Ideal output wanted:

      Date       Status   Amount
0     06-10-2000 Deposit $40.00
1  09-12-2002 Withdraw $1000.00
2     27-06-2001 Deposit $47.00
3   07-12-2021 Withdraw $100.00
4    06-10-2022 Deposit $120.00
5     06-10-2000 Deposit $40.00
6    09-12-2024 Withdraw $50.00

Upvotes: 0

Views: 35

Answers (2)

Scott Boston
Scott Boston

Reputation: 153460

You can use named groups and extract:

df[1].str.extract('(?P<Date>.*) (?P<Status>.*) (?P<Amount>.*)')

Output:

         Date    Status    Amount
0  06-10-2000   Deposit    $40.00
1  09-12-2002  Withdraw  $1000.00
2  27-06-2001   Deposit    $47.00
3  07-12-2021  Withdraw   $100.00
4  06-10-2022   Deposit   $120.00
5  06-10-2000   Deposit    $40.00
6  09-12-2024  Withdraw    $50.00

Upvotes: 2

Mayank Porwal
Mayank Porwal

Reputation: 34056

Assuming the separator in the column is a whitespace, you can use str.split with expand=True:

df[['col1','col2','col3']] = df['column'].str.split(expand=True)

Upvotes: 1

Related Questions