[SOLVED] How to add rows according to other column

Issue

This Content is from Stack Overflow. Question asked by Simon

now the result looks like this
enter image description here

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



Solution

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')


This Question was asked in StackOverflow by Simon and Answered by HY_712 It is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.

people found this article helpful. What about you?