Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

stop duplicating specific column values in explode()

You can even give alternative option than explode().

DataFrame:

A = ['ABC123', 'ABC124', 'ABC125', 'ABC126', 'ABC127']
B = ['ABC12', 'ABC13', 'ABC14', 'ABC15', 'ABC15']
C = ['aa, AA', 'bb', 'cc', 'dd', 'ee, EE']
D = [20, 30, 50, 54, 58]
df = pd.DataFrame({'A':A, 'B':B, 'C':C, 'D':D})

using explode, I am trying to make seprate row for the column ‘C’ values. i.e, ‘aa, AA’

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

df['C'] = df['C'].apply(lambda x: x.split(","))
df = df.explode(['C'])

and after explode DF something looking like this:

        A   B       C   D
0   ABC123  ABC12   aa  20
0   ABC123  ABC12   AA  20
1   ABC124  ABC13   bb  30
2   ABC125  ABC14   cc  50
3   ABC126  ABC15   dd  54
4   ABC127  ABC15   ee  58
4   ABC127  ABC15   EE  58

Issue I wish duplicated all column values expect column ‘D’, instead give 0

Desire output

        A   B       C   D
0   ABC123  ABC12   aa  20
0   ABC123  ABC12   AA  00
1   ABC124  ABC13   bb  30
2   ABC125  ABC14   cc  50
3   ABC126  ABC15   dd  54
4   ABC127  ABC15   ee  58
4   ABC127  ABC15   EE  00

>Solution :

Although not impossible, it would be less practical/efficient to prevent exploding of D, instead you can post-process the DataFrame to correct the values after explode.

Take advantage of the exploded index that will be duplicated to perform boolean indexing:

df.loc[df.index.duplicated(), 'D'] = 0

output:

        A      B    C   D
0  ABC123  ABC12   aa  20
0  ABC123  ABC12   AA   0
1  ABC124  ABC13   bb  30
2  ABC125  ABC14   cc  50
3  ABC126  ABC15   dd  54
4  ABC127  ABC15   ee  58
4  ABC127  ABC15   EE   0
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading