'Duplicate and rename column value where condition is satisfied in dataframe
| age | gender | occupation |
|---|---|---|
| 19 | Female | High School |
| 45 | Male | Designer |
| 34 | Both Gender | Coder |
This is how my dataframe looks; wherever the gender is 'Both Gender' I would like to duplicate the rows and create a row for both genders.
Expected Result -
| age | gender | occupation |
|---|---|---|
| 19 | Female | High School |
| 45 | Male | Designer |
| 34 | Female | Coder |
| 34 | Male | Coder |
Solution 1:[1]
dict1 = {"age": [19,45,34],
"gender": ["F","M","B"],
"occupation" : ["HS","grad","Coder"]}
df1 = pd.DataFrame(dict1)
df = df1[df1.gender == "B"]
df2 = df1[df1.gender == "B"]
df1 = df1[(df1.gender == "M") | (df1.gender == "F")]
df.gender = "M"
df2.gender = "F"
df3 = pd.concat([df1,df,df2])
df3
I think the above code will work.
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 | Yagami_Light |
