0

I have a pandas dataframe containing 3 columns.

This is what it looks like:

User History New
101  [X,Y,Z] [A-0,B-1] 
102  [Q,M,N] [A-1,B-0]   

I would like to modify my dataframe to be represented this way:

User History New 0or1
101  [X,Y,Z] A   0
101  [X,Y,Z] B   1
102  [Q,M,N] A   1
102  [Q,M,N] B   0

How can I do so?

Basically, the reason I’m doing this is because I’m trying to create a model which predicts 0 or 1 for each element in new based on the the history. Hence, I thought splitting them this way would make sense to train the model based on the three columns.

Though I was looking for ways to split the dataframe as described, I’m open to suggestions if there’s any other efficient way I can use the data provided (first table) to create a model to predict 1 or 0 for each element in array ‘new’ for the respective history.

Thanks in advance.

7
  • look at DataFrame.explode and Dataframe.melt Commented Jul 28, 2020 at 18:58
  • 1st explode using df.explode then use pd.Series.str.split Commented Jul 28, 2020 at 19:11
  • are the columns History and New lists or strings? Commented Jul 28, 2020 at 19:14
  • @Onyambu they are lists Commented Jul 28, 2020 at 19:15
  • 1
    Yes, set expand parameter to True i.e pd.Series.str.split('-', expand=True) Commented Jul 28, 2020 at 19:16

1 Answer 1

1
import pandas as pd 
df1 = df.explode('New')
pd.concat([df1,df1.New.str.split('-', expand = True)],axis=1)
   User    History  New  0  1
0   101  [X, Y, Z]  A-0  A  0
0   101  [X, Y, Z]  B-1  B  1
1   102  [Q, M, N]  A-1  A  1
1   102  [Q, M, N]  B-0  B  0
Sign up to request clarification or add additional context in comments.

3 Comments

Damn it, you beat me on time ;) just add a .drop(columns='New') to remove the initial column
Works well! Also, I can rename the two new columns right?
df1.assign(**df1['New'].str.split('-',expand=True).rename(columns={0:'New', 1:'0or'}))

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.