简体   繁体   中英

Multiplicate pandas dataframe rows according to boolean columns

Let's take this sample dataframe:

df=pd.DataFrame({'Name':['A','B','C','D'], 'NoMatter':[1,2,3,4], 'Cat1':[0,1,1,0], 'Cat2':[1,1,0,0]})
  Name  NoMatter  Cat1  Cat2
0    A         1     0     1
1    B         2     1     1
2    C         3     1     0
3    D         4     0     0

Each name can have 0, 1 or 2 categories (in my real dataframe, I have many more). I would like to create a new dataframe having one row per name per category, converting the name value into name.cat. I could go through a for loop but I know it is not the optimal way to do, especially since my real dataframe is big. Do you know please a good way to proceed?

Expected output:

     Name  NoMatter
0  A.Cat2         1
1  B.Cat1         2
2  B.Cat2         2
3  C.Cat1         3

Try:

df = df.melt(["Name", "NoMatter"])
df = df[df.value > 0]
df.Name = df.Name + "." + df.variable
df = df[["Name", "NoMatter"]].sort_values(by="Name").reset_index(drop=True)
print(df)

Prints:

     Name  NoMatter
0  A.Cat2         1
1  B.Cat1         2
2  B.Cat2         2
3  C.Cat1         3

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM