程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Beijing, Beijing, using Python to analyze the high cost of living, it is not easy to make a living

編輯:Python

Recently found a magical website , The cost of living in most cities in the world can be queried , Including clothing, food, housing, transportation and so on , You can let me sit at home , Know the living conditions of people all over the world . harm , It's so salty to eat radish and light to worry about !

Let's take a look at this website , It's like this for a long time

You can see, for example, living in Beijing , The monthly cost of a family of four is 15W RMB , If you're a bachelor , Then not the rent , Also want to 4K Too much overhead , It seems that it is really great Beijing , It's not easy to live here !
Of course, there are a lot of interesting data on the website , You can explore

https://www.numbeo.com/cost-of-living/in/Beijing

Let's take a look , Living in Beijing , What is the basic necessities of life

Finally, we also made a large screen effect

Data capture

In fact, capturing data is relatively simple , Use it directly Pandas Of read_html Function , This is really a great tool

import pandas as pd
pd.set_option('display.max_rows', None)
pd.set_option('display.max_columns', None)
data = pd.read_html("https://www.numbeo.com/cost-of-living/in/Beijing", header=None)

The data we got are as follows

Next, you can show the life of Greater Beijing through charts in an all-round way

The basic necessities of life in Beijing

The first is the cost of buying clothes in Beijing


It can be seen that , Men's business shoes are the most expensive , And a lady's skirt is the tallest 400 element , It seems that people's consumption concept has changed , Women's money is no longer so easy to earn

Next comes the cost of meals

Two or three people , The per capita consumption level of medium-sized restaurants is the highest , Up to 350 element , But for the imperial capital , It doesn't seem like much .
Is it in eating , Don't Beipiao people pay special attention to it

Let's look at the cost of fruits and vegetables

The highest per kilogram are apples and oranges , overall , I feel good about my feet

Finally, let's look at the rental cost


so to speak , Renting a house is the data that can best reflect the life of the imperial capital , One room suite in the city center , Up to 1.2W, And the three bedroom suit can also achieve 2.5W Scary numbers , It seems that living in Beijing , Everything else was fine , This is the big problem

National rental data screen

Since housing is the most expensive data , So let's take a look at other major cities in the country , What is the cost of renting a house
Let's get the data first

# coding = utf-8
"""
======================
@author:luobo
@time:2021/9/11:9:44
@email:
@File: main.py
======================
"""
import pandas as pd
import re
import time
import json
city_list = ['Beijing', 'Shanghai', 'Shenzhen', 'Guangzhou', 'Hangzhou', 'Suzhou', 'Nanjing', 'Fuzhou-China', 'Dalian',
'Qingdao', 'Shenyang', 'Jinan-China', 'Changchun', 'Harbin-China', 'Zhengzhou-China', 'Lanzhou-China',
'Chengdu', 'Chongqing', 'Wuhan', 'Tianjin']
city_url = "https://www.numbeo.com/cost-of-living/in/"
rent_dict = {
}
rent_range_dict = {
}
for city in city_list:
city_url_final = city_url + city
print(city_url_final)
data = pd.read_html(city_url_final)
one_bedroom = data[1].loc[54].tolist()[1]
one_bedroom_range = data[1].loc[54].tolist()[2]
data = re.findall(r'[0-9,.]+', one_bedroom)[0].replace(',', '')
data_range_list = [i.replace(',', '') for i in re.findall(r'[0-9,.]+', one_bedroom_range)]
data_range_list.append(data)
rent_dict[city] = data
rent_range_dict[city] = data_range_list
time.sleep(10)
with open("rent_data1.json", 'w') as f:
json_str = json.dumps(rent_dict, indent=4, ensure_ascii=False)
f.write(json_str)
with open("rent_data_range.json", 'w') as f:
json_str = json.dumps(rent_range_dict, indent=4, ensure_ascii=False)
f.write(json_str)
if __name__ == '__main__':
pass

Run code , Two will be generated json file , They are the average cost of renting a house , The maximum and minimum costs have been

Let's write the interface to get the histogram first

@app.route("/get_chart_data")
def get_chart_data():
with open("rent_data.json") as load_f:
data = json.load(load_f)
chart_info = {
}
chart1_data = get_data(["Beijing", "Shanghai", "Shenzhen", "Guangzhou", "Hangzhou"], data)
chart2_data = get_data(["Suzhou", "Nanjing", "Fuzhou-China", "Dalian", "Qingdao"], data)
chart3_data = get_data(["Shenyang", "Jinan-China", "Changchun", "Harbin-China", "Zhengzhou-China"], data)
chart4_data = get_data(["Lanzhou-China", "Chengdu", "Chongqing", "Wuhan", "Tianjin"], data)
chart_info['chart1'] = chart1_data
chart_info['chart2'] = chart2_data
chart_info['chart3'] = chart3_data
chart_info['chart4'] = chart4_data
return jsonify(chart_info)

This interface will return the average rental cost of each city , On the front end, we use AJAX To call

$(function () {

$.ajax({

url: '/get_chart_data',
type: 'get',
dataType: 'json',
success: function (res) {

echarts_1(res['chart1']);
echarts_2(res['chart2']);
echarts_3(res['chart3']);
echarts_4(res['chart4']);
}
});
function echarts_1(data) {

// Based on the prepared dom, initialization echarts example 
var myChart = echarts.init(document.getElementById('echart1'));

In this js In file , We go through echarts To fill in the different on the page id The corresponding area

Next, write the direct comparison data interface of each city

@app.route("/get_pie_data")
def get_pie_data():
with open("rent_data_range.json") as load_f:
data = json.load(load_f)
chart_info = {
}
data_list = []
for i in city_list:
data_list.append(data[i])
chart_info["k"] = city_list
chart_info["d"] = data_list
return jsonify(chart_info)

Corresponding echarts The code part is as follows

var chartDom = document.getElementById('map_1');
var myChart = echarts.init(chartDom);
var option;
var data = data["d"];
var cities = data['k'];
var barHeight = 50;
option = {

legend: {

show: true,
data: [' A price range ', ' mean value ']
},
grid: {

top: 100
},
angleAxis: {

type: 'category',
data: cities
},
tooltip: {

show: true,
formatter: function (params) {

var id = params.dataIndex;
return cities[id] + '<br> The minimum :' + data[id][0] + '<br> The highest :' + data[id][1] + '<br> Average :' + data[id][2];
}
},
...
option && myChart.setOption(option);

Finally, we finish flask Writing of basic code , The whole big screen is finished

from flask import Flask, render_template, jsonify
import json
app = Flask(__name__)
city_list = ['Beijing', 'Shanghai', 'Shenzhen', 'Guangzhou', 'Hangzhou', 'Suzhou', 'Nanjing', 'Fuzhou-China', 'Dalian',
'Qingdao', 'Shenyang', 'Jinan-China', 'Changchun', 'Harbin-China', 'Zhengzhou-China', 'Lanzhou-China',
'Chengdu', 'Chongqing', 'Wuhan', 'Tianjin']
@app.route("/")
def index():
return render_template("chengben.html")
...
if __name__ == '__main__':
app.run(debug=True, host='0.0.0.0')

About Python Technology reserve

Learn from good examples Python Whether it's employment or sideline, it's good to make money , But learn to Python Still have a learning plan . Finally, let's share a complete set of Python Learning materials , For those who want to learn Python Let's have a little help !

One 、Python Learning routes in all directions

Python All directions are Python Sort out the common technical points , Form a summary of knowledge points in various fields , The use of it is , You can find the corresponding learning resources according to the above knowledge points , Make sure you learn more comprehensively .

Two 、 Learning software

If a worker wants to do a good job, he must sharpen his tools first . Study Python Common development software is here , It saves you a lot of time .

3、 ... and 、 Getting started video

When we were watching videos to learn , You can't just move your eyes and brain without hands , A more scientific way to learn is to use them after understanding , At this time, the hand training program is very suitable .

Four 、 Practical cases

Optical theory is useless , Learn to knock together , Do it , Can you apply what you have learned to practice , At this time, we can make some practical cases to learn .

5、 ... and 、 Interview information

We learn Python Must be to find a well paid job , The following interview questions are from Ali 、 tencent 、 The latest interview materials of big Internet companies such as byte , And the leader Ali gave an authoritative answer , After brushing this set of interview materials, I believe everyone can find a satisfactory job .


This full version of Python A full set of learning materials has been uploaded CSDN, Friends can scan the bottom of wechat if necessary CSDN The official two-dimensional code is free 【 Guarantee 100% free


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved