I would like to get from this:
nname | eemail | email2 | email3 | email4 |
---|---|---|---|---|
Stan | [email protected] | NO | [email protected] | NO |
Danny | [email protected] | [email protected] | [email protected] | [email protected] |
Elle | [email protected] | NO | NO | NO |
To this:
nname | eemail |
---|---|
Stan | [email protected] |
Stan | [email protected] |
Danny | [email protected] |
Danny | [email protected] |
Danny | [email protected] |
Danny | [email protected] |
Elle | [email protected] |
I know I can create 4 separate DFs with name and email column, then merge all 4 and drop the ones with 'NO' but I feel there might be smarter and more dynamic solution for this.
CodePudding user response:
result = (
df.set_index("nname")
.stack()
.to_frame("eemail")
.query("eemail != 'NO'")
.droplevel(1)
.reset_index()
)
CodePudding user response:
Try this:
(df.mask(df.eq('NO'))
.set_index('nname')
.stack()
.droplevel(1)
.reset_index(level=0,name = 'eemail'))
Output:
nname eemail
0 Stan [email protected]
1 Stan [email protected]
2 Danny [email protected]
3 Danny [email protected]
4 Danny [email protected]
5 Danny [email protected]
6 Elle [email protected]