'Multiply rows in pandas DataFrame depending on values from c
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.
Solution 1:[1]
result = (
df.set_index("nname")
.stack()
.to_frame("eemail")
.query("eemail != 'NO'")
.droplevel(1)
.reset_index()
)
Solution 2:[2]
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]
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Code Different |
| Solution 2 | rhug123 |
