Python 为每组id号计算一些字符串值

Python 为每组id号计算一些字符串值,python,pandas,dataframe,pandas-groupby,Python,Pandas,Dataframe,Pandas Groupby,我有以下表格中的数据: import pandas as pd people_num=[1,1,2,2,2] visited_places=['school', 'restaurant', 'church', 'restaurant', 'church'] df= pd.DataFrame({'people_num':people_num, 'visited_places':visited_places}) 我想添加一个新列,其中包含每个人名的字符串计数数 为了形象化,原始表格如下所示: +

我有以下表格中的数据:

import pandas as pd
people_num=[1,1,2,2,2]
visited_places=['school', 'restaurant', 'church', 'restaurant', 'church']

df= pd.DataFrame({'people_num':people_num, 'visited_places':visited_places})
我想添加一个新列,其中包含每个人名的字符串计数数

为了形象化,原始表格如下所示:

+------------+----------------+
| people_num | visited_places |
+------------+----------------+
|          1 | school         |
|          1 | restaurant     |
|          2 | church         |
|          2 | restaurant     |
|          2 | church         |
+------------+----------------+
我想从中得到的新表是:

+------------+------------------------------+
| people_num |       counts_of_places       |
+------------+------------------------------+
|          1 | {'school':1,'restaurant':1}  |
|          2 | {'church':2, 'restaurant':1} |
+------------+------------------------------+

提前谢谢。

我认为您不应该将字典放入数据帧列中

我提出以下备选方案

>>> df
   people_num visited_places
0           1         school
1           1     restaurant
2           2         church
3           2     restaurant
4           2         church
>>> df.groupby('people_num')['visited_places'].value_counts().unstack(fill_value=0)
visited_places  church  restaurant  school
people_num                                
1                    0           1       1
2                    2           1       0
。。。但是如果你坚持的话


我认为你不应该把字典放到数据框列中

我提出以下备选方案

>>> df
   people_num visited_places
0           1         school
1           1     restaurant
2           2         church
3           2     restaurant
4           2         church
>>> df.groupby('people_num')['visited_places'].value_counts().unstack(fill_value=0)
visited_places  church  restaurant  school
people_num                                
1                    0           1       1
2                    2           1       0
。。。但是如果你坚持的话


啊!!你的替代方案听起来好多了。我将更容易地为其他栏中具有相同价值观的“一群人”总结访问过的地方的数量!再次感谢你!您可以将fill_值传递给.unstack,这样您就可以避免使用fillna,并在需要时将内容保留为int,例如:df.groupby'people_num'['visted_places'].value_计数。unstackfill_值=0Ah!你的替代方案听起来好多了。我将更容易地为其他栏中具有相同价值观的“一群人”总结访问过的地方的数量!再次感谢你!您可以在此处将fill_值传递给.unstack,这样您就可以避免使用fillna,并在需要时将内容保留为int,例如:df.groupby'people_num'['visted_places'].value_counts.unstackfill_value=0