Home >Backend Development >Python Tutorial >How Can I Use Pandas Groupby to Join Values with a Delimiter?

How Can I Use Pandas Groupby to Join Values with a Delimiter?

DDD
DDDOriginal
2024-12-09 15:49:14831browse

How Can I Use Pandas Groupby to Join Values with a Delimiter?

Pandas Groupby with Delimiter Join

Using the Pandas library, you can group rows with multiple values using the groupby function. However, by default, the values are concatenated without a delimiter. This article addresses the issue of introducing a delimiter to separate the values within each group.

You initially attempted to use the apply() function to join the values with a dash (-), but this resulted in the entire string being concatenated instead of separating the individual values.

A more straightforward approach is to use the agg() function with the join parameter. Here's how you can achieve the desired output:

group = df.groupby('col')['val'].agg('-'.join)

This will join the values within each group using a dash as the delimiter. The result will be:

col
A    Cat-Tiger
B     Ball-Bat

Note that the index is still present in the output, if you want to convert it to a column, you can use the reset_index() function:

df1 = group.reset_index(name='new')

This will convert the index to a new column named new. The final output will be:

  col  new
0   A  Cat-Tiger
1   B  Ball-Bat

Alternatively, you can use the squeeze() function (note this function was made as_nunique function in Pandas 1.4.0) to remove the index entirely and obtain a Series object:

group.squeeze()

This will result in a Series with the grouped values joined by the specified delimiter:

col
A    Cat-Tiger
B     Ball-Bat
Name: val

The above is the detailed content of How Can I Use Pandas Groupby to Join Values with a Delimiter?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn