1

I have a dataset like this

index  data                    data_list                    label
A      i love pyhton.          ['i love python.',           [1,0] 
       pyhton very useful.      'python very useful.']
B      i like tea. tea         ['i like tea.','tea          [0,1]
       make me happy.            make me happy.']

I want to arrange the dataset like below

index  data                    data_list                    label
A      i love pyhton.          i love python.               1 
       pyhton very useful.               
A      i love pyhton.          python very useful.          0
       pyhton very useful.      
B      i like tea. tea         i like tea.                  0
       make me happy.            
B      i like tea. tea         tea make me happy.           1 
       make me happy.            

But, i don't know how to solve this problem. Can anyone help, please. Thanks for your time.

Sari Devi
  • 15
  • 3

2 Answers2

1

You can try pandas.DataFrame.explode. Docs here.

alexm
  • 41
  • 1
  • 6
  • 3
    that should be a comment. I bet it won't take you too long to get sufficient rep. – FObersteiner May 23 '22 at 13:26
  • Yes, I agree it should be a comment, but I wanted to help and at that moment I don't have enough rep. Sorry for the noise :) – alexm May 23 '22 at 13:29
0

What you want is something called, explode function.Which basically expands, the interable and creates a base index to which grouped by element row. Just do the following line of code.

df.explode(['data_list','label'])
INGl0R1AM0R1
  • 328
  • 1
  • 11