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
Release: 2024-12-09 15:49:14
Original
839 people have browsed it

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)
Copy after login

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
Copy after login

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')
Copy after login

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
Copy after login

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()
Copy after login

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

col
A    Cat-Tiger
B     Ball-Bat
Name: val
Copy after login

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 of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template