How to combine multiple rows of strings into one using pandas?

前端 未结 4 1491
情话喂你
情话喂你 2021-02-01 19:12

I have a DataFrame with multiple rows. Is there any way in which they can be combined to form one string?

For example:

     words
0    I, will, hereby
1         


        
4条回答
  •  面向向阳花
    2021-02-01 19:41

    For anyone want to know how to combine multiple rows of strings in dataframe,
    I provide a method that can concatenate strings within a 'window-like' range of near rows as follows:

    # add columns based on 'windows-like' rows
    df['windows_key_list'] = pd.Series(df['key'].str.cat([df.groupby(['bycol']).shift(-i)['key'] for i in range(1, windows_size)], sep = ' ')
    

    Note: This can't be reached by groupby, because we don't mean the same id of rows, just near rows.

提交回复
热议问题