Skip to content
Advertisement

combine multiple lines into one column

Here is my dataframe following the merging of two separate dataframes.

In the [order] column, orders 2, 4 and 5, have multiple corresponding amounts in the [value] column. This is replicated elsewhere in the dataframe at random points. What can I do to merge these amounts so that I am left with one row in the dataframe per order:

Essentially, how do I sum the values against each order line leaving me with one amount in the [value] columns for each row of the dataframe?

enter image description here

So my desired output will look something like:

JavaScript

Advertisement

Answer

Use if order is column use GroupBy.transform with DataFrame.drop_duplicates:

JavaScript

If order is level of MultiIndex:

JavaScript
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement