Solving problem is about exposing yourself to as many situations as possible like Pandas groupby with delimiter join and practice these strategies over and over. With time, it becomes second nature and a natural way you approach any problems in general. Big or small, always start with a plan, use other strategies mentioned here till you are confident and ready to code the solution.
In this post, my aim is to share an overview the topic about Pandas groupby with delimiter join, which can be followed any time. Take easy to follow this discuss.
I tried to use groupby to group rows with multiple values.
col val A Cat A Tiger B Ball B Bat import pandas as pd df = pd.read_csv("Inputfile.txt", sep='t') group = df.groupby(['col'])['val'].sum()
A CatTiger B BallBat
I want to introduce a delimiter, so that my output looks like
A Cat-Tiger B Ball-Bat
group = df.groupby(['col'])['val'].sum().apply(lambda x: '-'.join(x))
A C-a-t-T-i-g-e-r B B-a-l-l-B-a-t
What is the issue here ?
Alternatively you can do it this way:
In : df.groupby('col')['val'].agg('-'.join) Out: col A Cat-Tiger B Ball-Bat Name: val, dtype: object
UPDATE: answering question from the comment:
In : df Out: col val 0 A Cat 1 A Tiger 2 A Panda 3 B Ball 4 B Bat 5 B Mouse 6 B Egg In : df.groupby('col')['val'].agg('-'.join) Out: col A Cat-Tiger-Panda B Ball-Bat-Mouse-Egg Name: val, dtype: object
Last for convert index or MultiIndex to columns:
df1 = df.groupby('col')['val'].agg('-'.join).reset_index(name='new')
group = df.groupby(['col'])['val'].apply(lambda x: '-'.join(x))