Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Yes, Pandas has a comparable feature to uncount in tidyr, which is the melt function. The melt function can transform a wide DataFrame into a long DataFrame by unpivoting.

For example, consider a DataFrame where the columns are different subjects, and each row contains the scores of a student for those subjects:

import pandas as pd

df = pd.DataFrame({"Name":["John","Sally","Bob"],
                   "Math":[80,90,70],
                   "Science":[75,85,80],
                   "History":[85,75,90]})
    Name  Math  Science  History
0   John    80       75       85
1  Sally    90       85       75
2    Bob    70       80       90



md5-cc6332462a710c26d0b6d48f083dd918



df_melted = df.melt(id_vars=["Name"],
                    value_vars=["Math","Science","History"],
                    var_name="Subject",
                    value_name="Score")



md5-a176c7e25ad7ea91f1c37ea95c88c51b



    Name  Subject  Score
0   John     Math     80
1  Sally     Math     90
2    Bob     Math     70
3   John  Science     75
4  Sally  Science     85
5    Bob  Science     80
6   John  History     85
7  Sally  History     75
8    Bob  History     90

Now the DataFrame is in long format, with separate rows for each subject and score of each student.