Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/329.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 功能操作_Python_Python 3.x_Pandas_Sorting_Group By - Fatal编程技术网

Python 功能操作

Python 功能操作,python,python-3.x,pandas,sorting,group-by,Python,Python 3.x,Pandas,Sorting,Group By,我有一个数据集,看起来像: 我想为每个原产地城市的名称加总,计算该城市唯一运营商的数量。例如: 德克萨斯州达拉斯/沃思堡:假设他们有4家不同的独特航空公司,如美国航空公司、达美航空公司、西南航空公司和捷蓝航空公司 那么输出应该如下所示: Out[1]: ORIGIN_CITY_NAME # of unique carriers 0 Dallas/Fort Worth,TX 4 1 San Francisco, CA

我有一个数据集,看起来像:

我想为每个原产地城市的名称加总,计算该城市唯一运营商的数量。例如:

德克萨斯州达拉斯/沃思堡:假设他们有4家不同的独特航空公司,如美国航空公司、达美航空公司、西南航空公司和捷蓝航空公司

那么输出应该如下所示:

    Out[1]:
    ORIGIN_CITY_NAME        # of unique carriers 
0   Dallas/Fort Worth,TX            4
1   San Francisco, CA               5 
...
有什么想法吗?我试过:


所以你需要像努尼克一样的东西

df.groupby('ORIGIN_CITY_NAME')['UNIQUE_CARRIER'].nunique()