我有一个代码,它可以组合所有的名字,计算它们的统计数据,并计算它们出现的次数。我在这里又加了一个专栏。如何从不同的数据帧打印最常见的名称?在我的代码中,这意味着我想找到voivodships最常见的汽车品牌
我收集了如下数据:
'Mercedes-Benz Klasa S', 4390.0, '(Dolnośląskie)']
['Mitsubishi Carisma 1.8', 3999.0, '(Pomorskie)']
['Audi A4 1.6', 4000.0, '(Łódzkie)']
for offer in all_offers:
# get the interesting data and write to file
title = offer.find('a', class_='offer-title__link').text.strip()
price = offer.find('span', class_='offer-price__number').text.strip().replace(' ', '').replace('\nPLN', '').replace('\nEUR', '')
location = offer.find('span', class_='ds-location-region').text.strip()
item = [title, float(price.replace(",", ".") ),location]
data.append(item)
print(item)
for name in [
'Alfa Romeo','Aston Martin', 'Audi',']:
print('---', name,'pojemność od 2000cm3' '---')
cars = df[df['title'].str.contains(name)]
print('count:', len(cars))
print('price min :', cars['price'].min())
print('price average:', cars['price'].mean())
print('price max :', cars['price'].max())
cars.plot.hist(title=name)
plt.show()
还有voivodships
for voivodships in ['('(Śląskie)', '(Świętokrzyskie)', '(Warmińsko-mazurskie)', '(Wielkopolskie)','(Zachodniopomorskie)']:
locations= df[df['location'].str.contains(voivodships)]
print('---', voivodships, '---')
print('ilość ogłoszeń w :', len(voivodships))
我认为您不需要循环,也不应该使用循环
这里有一个只使用熊猫的解决方案。我扩展了你的数据集
请参阅内联注释以获取进一步解释。结果:
如果您只是想查看按地区划分的最常见的品牌,可以这样做:
在这里,您按区域分组,然后在此组中,计算make的值,并取第一个、最频繁的值
相关问题 更多 >
编程相关推荐