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

Numpy, pandas merge rows

Im working on numpy, pandas and need to "merge" rows. I have column martial-status and there are things like this:

'Never-married', 'Divorced', 'Separated', 'Windowed'

and:

'Married-civ-spouse','Married-spouse-absent', 'Married-AF-spouse'

Im wondering how to merge them to just 2 rows, for the first 4 to single and for the second one’s in relationship. I need it for one hot encoding later.

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

enter image description here

And for sample output the martial-status should be just single or in relationship adequately to what i mention before

>Solution :

You can use pd.Series.map to convert certain values to other. For this you need a dictionary, that assigns each value with a new value. The values not presented in the dictionary will be replaced with NaN

married_map = {
    status:'Single' 
    for status in ['Never-married', 'Divorced', 'Separated', 'Widowed']}
married_map.update({
    status:'In-relationship' 
    for status in ['Married-civ-spouse','Married-spouse-absent', 'Married-AF-spouse']})
df['marital-status'].map(married_map)
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