Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/17.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 3.x 按一列对DataFrame进行分组,然后从另一列获取这些类别中出现的值列表_Python 3.x_Pandas - Fatal编程技术网

Python 3.x 按一列对DataFrame进行分组,然后从另一列获取这些类别中出现的值列表

Python 3.x 按一列对DataFrame进行分组,然后从另一列获取这些类别中出现的值列表,python-3.x,pandas,Python 3.x,Pandas,我正在寻找一种可能性,通过一个(或多个)列对一个数据帧进行分组,然后将另一列添加到分组的数据帧中,这样可以从原始数据帧中的另一列中获取该类别中出现的值。(下面的例子可能更容易理解我想做什么。) 例如,我有一个数据框,其中包含一些汽车的颜色和位置信息。我想知道每种颜色的车有多少辆(为此我使用groupby,但我愿意接受其他建议),但我也想得到这些车所在城市的列表 import pandas as pd df = pd.DataFrame({'cars': ['A','B','C', 'D', '

我正在寻找一种可能性,通过一个(或多个)列对一个数据帧进行分组,然后将另一列添加到分组的数据帧中,这样可以从原始数据帧中的另一列中获取该类别中出现的值。(下面的例子可能更容易理解我想做什么。)

例如,我有一个数据框,其中包含一些汽车的颜色和位置信息。我想知道每种颜色的车有多少辆(为此我使用groupby,但我愿意接受其他建议),但我也想得到这些车所在城市的列表

import pandas as pd

df = pd.DataFrame({'cars': ['A','B','C', 'D', 'E'], 'color':['blue','red', 'blue', 'red', 'blue'], 'city':['X', 'Y', 'X', 'Z', 'Z']})

df =
  cars city color
0    A    X  blue
1    B    Y   red
2    C    X  blue
3    D    Z   red
4    E    Z  blue

new_df = df.groupby(['color']).size().reset_index().rename(columns={0:'nr_of_cars'})

new_df = 
  color  nr_of_cars
0  blue           3
1   red           2
因此,在我的_df中,我有每种颜色的汽车数量,但我也想知道这些汽车所在的城市。一个新的数据框最终看起来是这样的(我并不需要那些城市在同一个数据框中,我只需要轻松地访问它们):

我所知道的是,我可以对每种颜色进行条件选择

other_df = df[df['color'] == 'blue']['city'].unique()
但是有没有一种方法可以让我不必循环浏览颜色列表呢?我真正的数据帧要大一点,所以我很乐意收到一些建议

编辑:只是修正了打字错误。

IIUC:

In [90]: df.groupby('color').agg({'cars':'size','city':'unique'}).reset_index()
Out[90]:
  color  cars    city
0  blue     3  [X, Z]
1   red     2  [Y, Z]
@狄龙

如果要查看所有可用的聚合方法(函数)和属性,请尝试使用
ipython
Jupyter
,如下所示:

首先创建一个“GroupBy”对象:

然后键入
g.
并按
键:

In [92]: g.
    g.agg        g.apply      g.cars       g.corrwith   g.cummax     g.describe   g.ffill      g.get_group  g.idxmax     g.mad        g.min
    g.aggregate  g.backfill   g.city       g.count      g.cummin     g.diff       g.fillna     g.groups     g.idxmin     g.max        g.ndim
    g.all        g.bfill      g.color      g.cov        g.cumprod    g.dtypes     g.filter     g.head       g.indices    g.mean       g.ngroup     >
    g.any        g.boxplot    g.corr       g.cumcount   g.cumsum     g.expanding  g.first      g.hist       g.last       g.median     g.ngroup

你知道在哪里可以找到所有聚合类型的列表吗?@Dillon,你是说聚合函数吗?是的“函数”不是types@Dillon, . 如果您想查看可用方法和属性的完整列表,请按照答案中的说明操作……太棒了,谢谢!我还没有意识到这个功能。
In [91]: g = df.groupby('color')
In [92]: g.
    g.agg        g.apply      g.cars       g.corrwith   g.cummax     g.describe   g.ffill      g.get_group  g.idxmax     g.mad        g.min
    g.aggregate  g.backfill   g.city       g.count      g.cummin     g.diff       g.fillna     g.groups     g.idxmin     g.max        g.ndim
    g.all        g.bfill      g.color      g.cov        g.cumprod    g.dtypes     g.filter     g.head       g.indices    g.mean       g.ngroup     >
    g.any        g.boxplot    g.corr       g.cumcount   g.cumsum     g.expanding  g.first      g.hist       g.last       g.median     g.ngroup