0

I have a data frame that has the following data

    Country Order Nr.
0   SU  123123
1   SU  235235
2   SU  442342
3   SU  545435
4   UA  656565
5   UA  142371
6   UA  142377

I need to merge all the "Order Nr." into a single row based on the country. The type of data in "Order Nr." is int while in "Country" it's a string

I have tried the following code but it does not seem to work for me

df.groupby('Order Nr.')[''].apply(' '.join).reset_index()

I get "sequence item 0: expected str instance, int found"

I need it to be in a data frame so that I can later convert them into an HTML and send it as an email. The expected output should be as follows:

    Country Order Nr.
0   SU  123123,235235,442342,545435
1   UA  656565,142371,142377

Please can anyone help

Thanks in Advance

  • 1
    You want to `groupby('Country')` then join the `'Order Nr.'` Series. See https://stackoverflow.com/questions/27298178/concatenate-strings-from-several-rows-using-pandas-groupby – ALollz Jun 04 '19 at 17:26
  • 1
    `df.astype(str).groupby('Country')['Order Nr.'].apply(','.join)` – anky Jun 04 '19 at 17:30
  • 1
    Hi @anky_91, Thank you very much for the help, your code worked. Had to just reset the index as it became a multilevel index – Mohamed rafiq Jun 04 '19 at 18:16

0 Answers0