Description
Pandas version checks
-
I have checked that this issue has not already been reported.
-
I have confirmed this bug exists on the latest version of pandas.
-
I have confirmed this bug exists on the main branch of pandas.
Reproducible Example
import pandas as pd
game = pd.DataFrame({
'team' : ['A', 'A', 'B', 'B', 'C', 'C', 'C'],
'members' : [1, 2, 3, 4, 5, 6, 7]
})
game['all_team_members'] = game.groupby('team').members.transform(lambda x : x.tolist())
game
Issue Description
I would like to concatenate the values from column 'members' by group 'team' to form a list, and attach them to a new variable all_team_members. I got the following results:
team members all_team_members
0 A 1 1
1 A 2 2
2 B 3 3
3 B 4 4
4 C 5 5
5 C 6 6
6 C 7 7
Expected Behavior
The expected results are:
team members all_team_members
0 A 1 [1, 2]
1 A 2 [1, 2]
2 B 3 [3, 4]
3 B 4 [3, 4]
4 C 5 [5, 6, 7]
5 C 6 [5, 6, 7]
6 C 7 [5, 6, 7]
Note, game.groupby('team').members.apply(lambda x : x.tolist())
worked as expected, so this problem is isolated to just the transform()
function
Installed Versions
pandas : 2.2.1
numpy : 1.24.3
pytz : 2023.3.post1
dateutil : 2.8.2
setuptools : 68.0.0
pip : 23.2.1
Cython : None
pytest : 7.4.0
hypothesis : None
sphinx : 5.0.2
blosc : None
feather : None
xlsxwriter : None
lxml.etree : 4.9.3
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 3.1.2
IPython : 8.15.0
pandas_datareader : None
adbc-driver-postgresql: None
adbc-driver-sqlite : None
bs4 : 4.12.2
bottleneck : 1.3.7
dataframe-api-compat : None
fastparquet : None
fsspec : 2023.4.0
gcsfs : None
matplotlib : 3.7.2
numba : 0.57.1
numexpr : 2.8.4
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : 11.0.0
pyreadstat : None
python-calamine : None
pyxlsb : None
s3fs : 2023.4.0
scipy : 1.11.1
sqlalchemy : None
tables : 3.8.0
tabulate : None
xarray : 2023.6.0
xlrd : None
zstandard : 0.19.0
tzdata : 2023.3
qtpy : None
pyqt5 : None