如何输出每个类别的餐厅数量(如:意大利、日本、中国)

Posted

技术标签:

【中文标题】如何输出每个类别的餐厅数量(如:意大利、日本、中国)【英文标题】:How to output the number of restaurants in each category (such as: Italian, Japanese, Chinese) 【发布时间】:2019-10-12 23:15:21 【问题描述】:

使用python和pandas,如何输出每个类别的餐厅数量?我有一个名为“Categories”的餐厅和列的数据集,其中包含“餐厅”,例如(意大利、中国......)。

我想从最受欢迎到最少的餐厅数排名前 10 位的餐厅数量。

数据存储在一个 csv 文件的变量“文件名”中。

我的方法:

def myrest(filename, city):

restaurants = filename[filename['categories'].str.contains('Restaurants')]
restaurants.loc[restaurants.categories.str.contains('Italian'),   'category'] = 'Italian'
restaurants.loc[restaurants.categories.str.contains('Japanese'), 'category'] = 'Japanese'

print(restaurants.category[:10])

输出应该是这样的: 意大利:350(350表示该市意大利餐厅的数量), 日语:250, 韩语:140, 土耳其语:77 ....

我只得到了餐馆的名称,而不是有多少餐馆的数量,例如“多伦多”。

【问题讨论】:

【参考方案1】:

如果要统计类别列中的值:

 restaurants.categories.value_counts()
 # or
 restaurants.groupby('categories').count()

你会得到一张餐厅类型的桌子和它在列中的次数。

【讨论】:

嗨@eitrheim 谢谢你的回答。我还想要每家餐厅的名称。例如:意大利语:210,日语:550 等

以上是关于如何输出每个类别的餐厅数量(如:意大利、日本、中国)的主要内容,如果未能解决你的问题,请参考以下文章

最全分析!解说中国何时超越美国成为世界科学中心?

组织能力:企业成败的关键(组织能力杨三角系列一)

C# EF Core 模型独立 FK

探店 | 上海素凯泰酒店La Scala餐厅新主厨 Antonio Corsaro 的新季菜单

上海素凯泰酒店标志性意大利餐厅La Scala 推出全新当季菜单

尝试设置子查询以将平均价格与给定类别的所有价格进行比较