Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/327.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 添加一列,其中包含ID';它来自另一个数据帧_Python_Python 3.x_Pandas_Pandas Groupby - Fatal编程技术网

Python 添加一列,其中包含ID';它来自另一个数据帧

Python 添加一列,其中包含ID';它来自另一个数据帧,python,python-3.x,pandas,pandas-groupby,Python,Python 3.x,Pandas,Pandas Groupby,问题 我想对数据帧执行groupby,结果数据帧包含一列,其元素是groupby参数的列表 示例 我有一个数据框ship\u cluster,其中包含ShipID,纬度,经度,以及一个名为cluster的列 In [4]: df = pd.DataFrame({"ShipID": [7, 7, 8, 9], "latitude": [51.872842, 51.872874, 51.872794, 51.872946],

问题
我想对数据帧执行
groupby
,结果数据帧包含一列,其元素是
groupby
参数的列表

示例
我有一个数据框
ship\u cluster
,其中包含
ShipID
纬度
经度
,以及一个名为
cluster
的列

In [4]: df = pd.DataFrame({"ShipID": [7, 7, 8, 9],
                           "latitude": [51.872842, 51.872874, 51.872794, 51.872946],
                           "longitude": [5.810379, 5.810729, 5.810754, 5.810548],
                           "cluster": [0, 1, 0, 0]})
print(df)

"ShipID" latitude  longitude cluster
7        51.872842 5.810379  0
7        51.872874 5.810729  1
8        51.872794 5.810754  0
9        51.872946 5.810548  0
我希望得到的预期结果是:

         latitude  longitude ShipID
cluster                            
0        51.872860 5.810560  [7, 8, 9]
1        51.872874 5.810729  [7]
因此,根据
集群
,我希望看到列表中的
ShipID
。显然,我可以先做一个groupby:

ship_cluster[["latitude", "longitude", cluster"]].groupby("cluster").mean()

但我不知道下一步,也不知道简化的方法。有什么帮助吗?

如果需要,我相信需要通过
集群聚合

d = {"latitude":'mean', "longitude":'mean', "ShipID":lambda x: x.tolist()}
df = ship_cluster.groupby("cluster").agg(d)
print (df)
         latitude  longitude ShipID
cluster                            
0        51.87270    5.81362    [7]
1        51.85040    5.86688    [7]
2        51.87410    5.91493    [7]
3        51.85500    5.96898    [7]
4        51.88101    6.00426    [7]
5        51.87368    6.03096    [7]
或通过
ShipID

d = {"latitude":'mean', "longitude":'mean', "cluster":lambda x: x.tolist()}
df = ship_cluster.groupby("ShipID").agg(d)
print (df)
         latitude  longitude             cluster
ShipID                                          
7       51.867815   5.933272  [0, 1, 2, 3, 4, 5]