Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Python to analyze all Pizza Hut restaurants in the country

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article is about how to use Python to analyze all Pizza Hut restaurants in the country. I think it is very practical, so I share it with you. I hope you can get something after reading this article. Let's take a look at it with the editor.

I talked about how to crawl information about restaurants in major cities across the country on the Pizza Hut website. Although the restaurant data information has been captured, but the data has been "lying dead" in the hard drive. I don't remember that I have done this for the nth time. Speaking of which, I have to go back to my college days.

I have been in contact with Python since college, and I was very curious at that time. Wonder why Python doesn't need a browser to crawl website data. I sighed in my heart that this was simply wonderful. In order to experience the fun of grabbing data, I wrote a lot of crawler programs.

With the expansion of my knowledge, I learned about the field of data analysis. Only then did I know that the crawled data had some hidden information behind it. I am also learning the relevant knowledge in this area. This article is the first draft of data analysis, the main content is to extract some information about Pizza Hut from the data.

01 environment building

Baidu front-end technology department open source a Javascript-based data visualization graph library. Its name is ECharts. It is a sharp tool for front-end data visualization, which can provide intuitive, vivid, interactive and customizable data visualization charts.

There is a great god in China who suddenly thought that such an easy-to-use library would be good if it could be combined with Python. As a result, the pyecharts library came into being. Therefore, the role of pyecharts is to generate a class library for Echarts diagrams. All the icons in this article are generated using pyecharts.

Installing the library is also easy, using pip to install.

Pip install pyecharts02 data cleaning

Data cleaning is an indispensable step in data analysis. This step is to clean some dirty data. Because it is possible that the site itself has free data, or matching crawling site information, there is some confusion in the data. All these need to be cleared away.

I used to write the data to a json file, and I read it out first. Then convert the json text data to a dictionary type.

Def get_datas ():

"" get data from file ""

File_name = 'results.json'

With open (file_name, 'ritual, encoding='UTF-8') as file:

Content = file.read ()

Data = json.loads (content, encoding='UTF-8')

# print (data)

Return data

Then the dictionary is traversed to count the total number of restaurants in each city.

Def count_restaurants_sum (data):

"traverse the dictionary to count the total number of restaurants in each city"

Results = {}

For key, value in data.items ():

Results [key] = len (value)

# print (key, len (value))

Return results

Each key-value in the dictionary is then converted to a tuple, and then sorted in reverse order according to value.

Restaurants_sum = sorted (restaurants_sum.items (), key=lambda item: item [1], reverse=True)

Finally, according to the display result, delete some dirty data manually.

Def clean_datas (data):

"

Clean up dirty data.

After analysis, it is found that ('New District', 189), ('South District', 189) and ('Chaoyang', 56) are dirty data. These three names are found in the regional options of Pizza Hut's official website.

[('New area', 189), ('Shanghai', 189), ('South District', 189), ('Beijing', 184), ('Shenzhen', 95)

(Guangzhou, 86), (Hangzhou, 78), (Tianjin, 69), (Chaoyang, 56), (Suzhou, 54)]

"

Data.remove (('New area', 189)

Data.remove (('Southern District', 189)

Data.remove ((Chaoyang, 56))

Return data

At this point, the data work has been completed.

03 data analysis

We have got the cleaned data, we simply print the data, and then draw a histogram.

Def render_top10 ():

"

Cities that draw a histogram showing the total number of Pizza Hut restaurants in the country Top 10

According to the results of the cleaned data, the Top cities are as follows

(Shanghai, 189), (Beijing, 184), (Shenzhen, 95), (Guangzhou, 86), (Hangzhou, 78)

('Tianjin', 69), 'Suzhou', 54), ('Xi'an', 52), ('Wuhan', 51), ('Chengdu', 48)

"

Attr = ["Shanghai", "Beijing", "Shenzhen", "Guangzhou", "Hangzhou", "Tianjin", "Suzhou", "Xi'an", "Wuhan", "Chengdu"]

Values = [189,184,95,86,78, 69,54,52,51,48]

Bar = Bar ("ranking of Pizza Hut restaurants in major cities in China")

Bar.add ("Total", attr, values, is_stack=True, is_more_utils=True)

Bar.render ("render_bar.html")

The plotted results are as follows:

It is not difficult to see that first-tier cities have more Pizza Hut restaurants, and provincial capital cities have more restaurants than non-provincial capital cities.

Let's continue to draw a pie chart to see the proportion of restaurants in the north, Guangzhou and Shenzhen in the whole country.

Def render_top10_percent ():

"

Draw a pie chart to show the proportion of the number of restaurants in Beijing, Shanghai, Guangzhou and Shenzhen in the country.

"

Configure (global_theme='macarons')

Attr = ["Shanghai", "Beijing", "Shenzhen", "Guangzhou", "other cities"]

Value = [189,184,95,86,1893] # calculated according to count_other_sum ()

Pie = Pie ("proportion of the number of restaurants in Beijing, Guangzhou and Shenzhen")

Pie.add ("", attr, value, is_label_show=True, is_more_utils=True)

Pie.render ("render_pie.html")

The plotted results are as follows:

According to the data, the number of restaurants in the north, Guangzhou and Shenzhen accounts for 22.64% of the total number of restaurants in the country. Other second-and third-tier cities account for 77.36%. It shows that Pizza Hut not only focuses on big city routes, but also develops to second -, third-and fourth-tier cities and expands its field.

The above is how to use Python to analyze all Pizza Hut restaurants in the country. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report