简体   繁体   中英

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在此处输入图像描述

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM