Latest news about Bitcoin and all cryptocurrencies. Your daily crypto news habit.
Python is in a great demand these days even after a decade of itâs release. It has become the first choice of every next coder as it is easy to learn. It is a perfect tool for the web programming job. In fact, it is also very popular among start-ups and data scientists. Due to its extreme logical and light features, Python is counted among top 5 coding languages which clearly indicates how impactful it will be in the coming years.
In addition, installation programs such as Anaconda can do incredible things with several operating systems (OS). Python has the added benefit of being a clean and simple syntax with the mass library managed by the community (NumPy, Pandas, SciPy, etc.).
This means that beginners and experienced veterans will have access to millions of lines of code that can be easily taken from highly skilled python developers (in the world) and used by anyone. In this blog, we will look at the top 3 things which can you do with Python programming language.
1) Data Visualization
When it comes to Data science, it is all about solving problems using a diverse set of tools. In fact, Data visualization is indicative of data science because a lot of time used to spend on collecting and formatting the data. But with the help of python programming, coders use a clean dataset i.e. seaborn and matplotlib both for visualizing the data.
For data visualization, you can also use built-in dataframe plot method as given below:
# To Make a horizontal bar chart (for salaries of all college presidents in Ohio )
df.plot(kind=âbarhâ, x = âPresidentâ, y = âsalaryâ)
However, it is a little bit complex task to plot code in Python development and requires a lot of practice to get things done accurately. You can also refer to the official documentation of Python programming or sites like Stack Overflow for technical concerns.
For better outcome, you can show how many students are paying for 5 minutes of their presidentâs time and so convert salaries into $ / five minutes assuming 2000 work hrs per day.
So, this is a good way to wrap up a small project of Data Science using python web development.
2) Web Scraping
It is not an easy task to pull data from the web. Therefore, web scraping is done to retrieve online data easily. Python offers great tools for this job like âbs4â (BeautifulSoup) for extracting the relevant information and ârequestsâ library for retrieving content from a webpage. These two libraries in python development are used in the following manner:
- The first step is to make a GET request to a website.
r = requests.get(âhttp://www.cleveland.com/metro/index.ssf/2017/12/case_western_reserve_university_president_barbara_snyders_base_salary_and_bonus_pay_tops_among_private_colleges_in_ohio.html')
- Now, create a Beautiful Soup object from the content which is returned.
soup = BeautifulSoup(c)
- Now, parse it using several methods.
# Extract the content
c = r.content
from bs4 import BeautifulSoup
The expected outcome is shown below:
Now, its time to extract the data. For this, you need to select from soup table, you need to find the correct CSS selectors. One way to do this is by going to the website and inspecting the item. In this case, you can also simply look at the soup and see that our table resides under an HTML <div> tag with the attribute class = âentry-contentâ. Using this information and the .find method of the soup object, you can extract the content of the main article.
However, this will return a soup object which is necessarily not enough for this job. To select the table, you need to find the <ul> tag. To deal with only the text in the table, you can use the .text attribute of the soup. The required code is given below:
content = main_content.find(âulâ).text
Now, you have the exact text of the table as a string but obviously, it is not of much use to you yet. To extract specific parts of a text string, you must move on to regular expressions which I have explained in next major point.
3) Regular Expressions
Regular expressions are used for parsing data in order to extract information. The basic idea behind regular expressions is to define a pattern which is used to match in a text string and then search in the string to return matches. But some of these patterns are quite different as they contain both the special characters which show how the pattern is interpreted and the content you want to match. It is a vital tool and comes up when it is the time for parsing string information. There are three pieces of information required in order to extract info from the table(the example of college presidents). These are mentioned below:
- Names of the colleges
- Names of the presidents
- Salaries
For the names of presidents, make use of the fact that each name is at the start of a line and ends with a comma. The code which will create a regular expression pattern and then searches with the help of the string to find the consequent patterns is given below:
# Create a pattern to match names
name_pattern = re.compile(râ^([A-Z]{1}.+?)(?:,)â, flags =Â re.M)
# Find all occurrences of the pattern
names = name_pattern.findall(content)
However, the pattern is quite complex but it can do all that you want actually from it. So the overall process is to first define the pattern and then search a string to find the pattern. The same process is repeated for the names of colleges and the salary. The code is given below:
# Make school pattern and extract schools
school_pattern = re.compile(râ(?:,|,\s)([A-Z]{1}.*?)(?:\s\(|:|,)â)schools = school_pattern.findall(content)
# Pattern to match the salaries
salary_pattern = re.compile(râ\$.+â)salaries = salary_pattern.findall(content)
To convert salaries into numbers, you can practice a Python list comprehension. For this you can use string slicing, join and split, all within a list comprehension to achieve the desired results.
Now, you have to apply this transformation to the salaries and finally get all the information you want to extract. You can also put everything in a panda data frame using Python programming. It is important to know when it is more efficient to do things by hand instead of writing a complicated program.
Some other exciting things you can do with Python programming language are mentioned below:
- Function Decorators
- Machine Learning
- Robotics
- Browser Automation
- Scientific & Numeric computing
Conclusion:
So we have seen various interesting things you can do with Python programming language. The language is evolving fast within the data science space.
The use of these data science tools has changed the whole dynamic of the average Python user. In fact, it is an ecosystem which is full of data science tools. It will soon replace many popular programming languages in the near future.
So, it is the right time to immediately switch to Python by hiring a reliable and good Python development company like ValueCoders which is one of the leading Python development companies in India that can help you for your Python development needs.
Top Three Things You Can Do With Python In 2018 was originally published in Hacker Noon on Medium, where people are continuing the conversation by highlighting and responding to this story.
Disclaimer
The views and opinions expressed in this article are solely those of the authors and do not reflect the views of Bitcoin Insider. Every investment and trading move involves risk - this is especially true for cryptocurrencies given their volatility. We strongly advise our readers to conduct their own research when making a decision.