Concatenate strings from several rows using Pandas groupby

后端 未结 4 1868
误落风尘
误落风尘 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 04:00

    we can groupby the 'name' and 'month' columns, then call agg() functions of Panda’s DataFrame objects.

    The aggregation functionality provided by the agg() function allows multiple statistics to be calculated per group in one calculation.

    df.groupby(['name', 'month'], as_index = False).agg({'text': ' '.join})

提交回复
热议问题