0

I have a sample of the dataframe as given below.

data = {'Participant':['A', 'B', 'C', 'D'],
    'Total Entry':[2, 3, 1, 3],
    'Time':[['2021-07-12', '2021-07-16'],['2021-07-21','2021-07-22','2021-07-24'],['2021-06-20'],['2021-07-26','2021-07-29','2021-08-01']],
    'Values':[['Negative','Negative'],['Positive','Positive','Negative'],['Negative'],['Negative','Positive','Negative']]}

df_test= pd.DataFrame(data)
df_test

enter image description here

I want to unpack corresponding values of 2 columns and append it to the corresponding rows. The final obtained result is shown below.

enter image description here

I tried using pop, unstack and Series functions, but it is not working. Any help is greatly appreciated. Thanks.

Shiva
  • 212
  • 2
  • 11

1 Answers1

0

Try:

df_test.explode(['Time', 'Values'])
MDR
  • 2,610
  • 1
  • 8
  • 18
  • I am getting an Value Error as given: "ValueError: column must be a scalar" – Shiva Aug 02 '21 at 20:16
  • Try separately first `df_test = df_test.explode('Time')` and then `df_test = df_test.explode('Values')` – MDR Aug 02 '21 at 20:22
  • It does not work as well because it gives all combination of 'Time' and 'Values' which results in a bigger dataframe of 23 rows . I require only corresponding values with 9 rows. – Shiva Aug 02 '21 at 20:28
  • 1
    Figured. `df_test.set_index(['Participant','Total Entry']).apply(pd.Series.explode).reset_index()` works. – Shiva Aug 02 '21 at 20:30