Concatenate strings from several rows using Pandas groupby

后端 未结 4 1951
误落风尘
误落风尘 2020-11-22 03:17

I want to merge several strings in a dataframe based on a groupedby in Pandas.

This is my code so far:

import pandas as pd
from io import StringIO

         


        
4条回答
  •  借酒劲吻你
    2020-11-22 03:54

    For me the above solutions were close but added some unwanted /n's and dtype:object, so here's a modified version:

    df.groupby(['name', 'month'])['text'].apply(lambda text: ''.join(text.to_string(index=False))).str.replace('(\\n)', '').reset_index()
    

提交回复
热议问题