Home >Backend Development >Python Tutorial >How to Combine Strings Within Groups Using Pandas groupby?
The provided DataFrame contains three columns: A, B, and C. The goal is to group the DataFrame by column A and obtain a union of strings from column C for each group.
By default, groupby sums numeric columns, which doesn't work for strings.
One approach is to define a function that concatenates strings within each group using the join method:
<code class="python">def f(x): return "{%s}" % ', '.join(x)</code>
And apply this function to the grouped DataFrame:
<code class="python">result = df.groupby('A')['C'].apply(f)</code>
This approach produces the desired output:
A 1 {This, string} 2 {is, !} 3 {a} 4 {random}
Another option is to force sum to concatenate strings by modifying the data type:
<code class="python">df['C'] = df['C'].astype(str) result = df.groupby('A')['C'].sum()</code>
This also gives the desired result.
The above is the detailed content of How to Combine Strings Within Groups Using Pandas groupby?. For more information, please follow other related articles on the PHP Chinese website!