Home > database >  Merge multiple columns in same DataFrame
Merge multiple columns in same DataFrame

Time:09-14

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?

CodePudding user response:

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
  • Related