How to add rows according to other column

Viewed 25

now the result looks like this

file_name    text                                           1
2a.txt         0      0.712518  0.61525  0.43918     0.2065 1     0.635078  0.81175  0.292786    0.0925
2b.txt         2      0.551273  0.5705   0.30198     0.0922 0     0.550212  0.31125  0.486563    0.2455

But I want duplicate rows according to the third column(as shown below), is there an easy way to do this?

file_name    text  
2a.txt         0      0.712518  0.61525  0.43918     0.2065
2a.txt         1      0.635078  0.81175  0.292786    0.0925
2b.txt         2      0.551273  0.5705   0.30198     0.0922
2b.txt         0      0.550212  0.31125  0.486563    0.2455
1 Answers

That should help:

df = pd.melt(df,id_vars='file_name' ,value_vars=['text','1'])
df = df.drop('variable', axis=1)
df = df.sort_values(by = 'file_name')
Related