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

Merge multiple columns in same DataFrame

I have the following dataframe with ID column as Index, and 3 numeric value columns.


index   column_A    column_B    column_C
9894    49.99        nan        nan
9894    nan          49.99      nan
9269    636.09       nan        nan
9269    nan          nan        3536.23
9269    nan          636.09     nan
8655    2861.11      nan        nan
8655    nan          2861.11    nan
8618    471.09       nan        nan
8618    nan          471.09     nan

My script basically creates a new column for each occurrence of the 3 columns, so per ID, I have 2 columns having nan, and 1 with the value. Like below
enter image description here

is there a way to merge or concat these set of columns into one row.. so that I have 1 row per ID with the 3 values on the same row?

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

>Solution :

it’ll be helpful if you add the expected output, but it seems you like as follows

df.groupby('index').sum().reset_index()
    index   column_A    column_B    column_C
0   8618     471.09       471.09       0.00
1   8655    2861.11      2861.11       0.00
2   9269     636.09       636.09    3536.23
3   9894      49.99        49.99       0.00
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