import pandas as pd
data = {
'Name': ['Alice', 'Alice', 'Charlie', 'Charlie'],
'Age': [25, 30, 35, 40],
'branch': ['New York', 'Los Angeles', 'Chicago', 'Houston']
}
df = pd.DataFrame(data)
# concatenate the string
df['branch'] = df.groupby(['Name'])['branch'].transform(lambda x : ' '.join(x))
# show the dataframe
print(df)
can anyone expain what’s happening when we apply transformation function on result of group by?the output i am getting is
Name | Age | branch |
---|---|---|
Alice | 25 | New York Los Angeles |
Alice | 30 | New York Los Angeles |
Charlie | 35 | Chicago Houston |
Charlie | 40 | Chicago Houston |