Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

pandas group by concat

In [124]:

df.groupby(['name','month'])['text'].apply(lambda x: ','.join(x)).reset_index()

Out[124]:
    name  month         text
0  name1     11       hej,du
1  name1     12        aj,oj
2  name2     11     fin,katt
3  name2     12  mycket,lite
Comment

Concatenate strings using Pandas groupby

In[38]:
df.groupby(['name','month'])['text'].apply(','.join).reset_index()

Out[38]: 
    name  month         text
0  name1     11           du
1  name1     12        aj,oj
2  name2     11     fin,katt
3  name2     12  mycket,lite
Comment

PREVIOUS NEXT
Code Example
Python :: time it in jupyter notebook 
Python :: sum of all nan values pandas 
Python :: between date pandas 
Python :: bmi python 
Python :: python print colored text 
Python :: how will you print space and stay on the same line in python 
Python :: os get current directory 
Python :: numpy from csv 
Python :: membercount discord.py 
Python :: python pendas shut off FutureWarning 
Python :: dataframe select entries that are in a list 
Python :: get current time in python with strftime 
Python :: python list of dates between 
Python :: df dropna ensure that one column is not nan 
Python :: convert string to unicode python 3 
Python :: how to make it so the pygame window will close 
Python :: python get copied text 
Python :: converting string array to int array python 
Python :: How to update python using anaconda/conda 
Python :: python cmd colors 
Python :: plotly plot size 
Python :: get xpath of element selenium python 
Python :: discord.py send image 
Python :: python code to drop columns from dataframe 
Python :: python format currency 
Python :: mp4 to mp3 in python 
Python :: kivymd simple button 
Python :: discord.py change status 
Python :: py for line in file 
Python :: pandas drop values from column 
ADD CONTENT
Topic
Content
Source link
Name
8+4 =