Preface
What is programming , To put it simply, it's actually to write code ? That's such a boring job , Why do so many people want to enter this industry ? Actually , As a professional for many years “ Senior cv The engineer ”, One College entrance examination 6 Institutions , Every institution 6 A professional , There's no one who has anything to do with computers , Up to now, I have completely fallen in love with the computer industry , It's very simple , When you see yourself writing countless lines of code , Finally, it shows the functions one by one demo, Help a lot of people save a lot of practice ,demo Isn't it our project to unite ? Right , In fact, the charm of programming is to teach the computer to help you do all kinds of difficult things in the shortest time , And then help others
Because I wrote some for you before Python Reptiles Related cases of , It's very hot recently python What can I do for you ? It doesn't seem to have much to do with my work , Take a moment to look down , except Artificial intelligence , big data Beyond such high-end disciplines , How does he help you in your daily life
First knowledge of data analysis
If you're an accountant , Administration , Journalism , In medicine and other professions, you can use data analysis to help you sort out and report a lot of data
What era is this , This is an age of data , You have to deal with a lot of data reports every day 、 Reports, even customer relationship news, etc , however , There is a saying. , The number is not as good as the watch , Chart is not as good as table , I don't know if your year-end report is finished , Let me take this year-end report as an example , In fact, it's plain , What the year-end report should do is to show the data of the whole year and the data of the future ? And the data , When we finish the presentation , What can you do , The previous data can be directly used out of the box , adopt excel Wait for the operation , Even though it's a little more troublesome , But it's also possible to make some forms (ps:office Except for the gods , I this office Small slag , Don't even think about it ), But when the amount of data comes up ,office Not even the great God , What's more, with the help of previous data , Show the future . What the year-end report decides is the amount of the year-end bonus , And your career development in the coming year , So at this point , Through data processing, filtering and visual analysis , Come to the conclusion , Help you make decisions
With all of you , The time to feel your head and make decisions is over , This is the age of data driven decision making , Or you write a year-end report , The boss asked you how you got this conclusion , You said you felt it ? If your whole report data derivation conclusion is in place , Are you more likely to add year-end bonus to your year-end report ?
So much for that ,python What can I do for you ?
Look directly at the renderings
That's our most basic , Draw our icon through the corresponding data , The source of the data is simple , It's America that can be downloaded from the Internet gdp Data graph , But if I really want you to do it , You're going to have a headache , If you don't believe it, you can try
This is the data format :
Data source address :https://www.kylc.com/stats/global/yearly_per_country/g_gdp/usa.html
I think it will take a lot of time just for you to paste and organize the data , however , The same thing , If you give it all to python To do it , How much time can I save you ? The first is our data acquisition
Crawler gets data code
# -*- coding: utf-8 -*-
import requests
from lxml import etree
import openpyxl
•
•
def get_html(url):
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.106 Safari/537.36"
}
html = requests.get(url)
return html.text
•
def parse_content(content):
e = etree.HTML(content)
year_list = e.xpath("//tr/td[1]/text()")
gdp_list = e.xpath("//tr/td[2]/text()")
percents_list = e.xpath("//tr/td[3]/text()")
year_list = list(filter(is_correct,year_list))
year_list = list(map(int,year_list))
gdp_list = list(map(extract, gdp_list))
percents_list = list(map(delete_char,percents_list))
print(year_list)
print(gdp_list)
print(percents_list)
save_data(year_list,gdp_list,percents_list)
•
•
def save_data(year_list,gdp_list,percents_list):
wk = openpyxl.Workbook()
sheet = wk.active
for i in range(len(year_list)):
sheet.append([year_list[i],gdp_list[i],percents_list[i]])
wk.save("t2.xlsx")
•
def extract(s):
return int(s[s.find('(')+1:s.rfind(')')].replace(",",""))
•
def delete_char(s):
return float(s.replace("%",""))
•
def is_correct(s):
s = s.strip()
if s:
return s.isdigit()
return False
•
if __name__ == '__main__':
content = get_html("https://www.kylc.com/stats/global/yearly_per_country/g_gdp/usa.html")
parse_content(content)
When our data is obtained and sorted according to certain rules , Specific steps , You can refer to my previous python Crawler related content
And when the data is available , We can analyze the data according to these data , I'll just show you some key codes , I have uploaded the detailed code to my code cloud warehouse , Friends in need can take their own :
Data analysis code
# Draw America GDP Change chart import xlrdimport matplotlib.pyplot as pltimport numpy as np
#1、 get data
data=xlrd.open_workbook(r'E:\Python\com\test\GDP\t4.xlsx')
#print(data.sheet_names()) # Read the data in the table excel=data.sheet_by_name('Sheet')
#print(excel.nrows)
#2、 Charting
# Set the font
plt.rcParams['font.sans-serif']=['FangSong']
# Draw area
plt.figure(figsize=(16,9))plt.title(" The United States gdp Change chart ")
# structure xy Axis
plt.grid(linestyle='-.')
plt.xlabel(' year ')
plt.ylabel('GDP( One trillion )')
#3、 The data fills the chart
# adopt for Loop through the data in each cell and store it accordingly
year_list=[]for i in range(excel.nrows):
year=excel.cell_value(i,0)
# Adds data from a cell to the list
year_list.append(year)
# !!!!!!!! The most important point : Merge data together to form a multidimensional array arr=np.array(list(zip(year_list,gdp_list,percent_list)),dtype=np.float64)
#4、 Exhibition
plt.plot(arr[:,[0]],arr[:,[1]]/1000000000000,"dg",label="GDP")
plt.plot(arr[:,[0]],arr[:,[2]],"--r",label=" Market share ")
#print(year_list)
plt.legend()
plt.show()
Be careful :
If you have the latest version numpy, But you also used the previous online tutorial , Will report a mistake
No problem , take it easy , No impact on operation , Just said it would report a mistake , The method of improvement is also very simple
The old version of the code says np.float To define the data type , After improvement, it's like this
That's all right.
This is also a problem brought about by code updating , A lot of previous code can't be used or error is reported , This question is very unfriendly for beginners , We may have to go through a lot of information , But I don't know , The reasons and answers are shown on the console
—END—
Here are some new year's benefits for you , There is a need for these learning materials , You can pay attention to me , Click add background assistant :python1180 Can get
This is my new year's benefit to you