Top 5 Python Libraries for Beginners.
Hello, Python enthusiasts!
I’m excited to share key concepts and examples of the top 5 Python libraries that can simplify your everyday tasks. Learning Python might seem challenging at first, but don’t worry! We’re here to break it down and make it easier for you.
1) NumPy
NumPy is a strong Python library for numerical computation, particularly with arrays and matrices. It enables you to do difficult mathematical operations efficiently and fast, making it perfect for activities such as linear algebra, statistics, and scientific calculations.
Examples
Calculating the total cost of ingredients in your restaurant.
You can run below python code in any IDE
Before running, simply give pip install numpy.
import numpy as np
ingredients = np.array([100, 150, 200]) # prices of rice, dal, and oil
total_cost = np.sum(ingredients)
print(total_cost)
# Output: 450
-
Let’s say you have an array of test scores, and you want to filter out all the scores that are greater than 50.
import numpy as np
scores = np.array([45, 67, 89, 34, 56, 90, 23,89,34,65,92,37,56])
passed = scores[scores > 50]
print(“Scores greater than 50:”, passed)
Output:
Scores greater than 50: [67 89 56 90 89 65 92 56]
-
You have a grid of sales data from 3 different shops for 4 months, and you want to find the total sales per shop and per month.
import numpy as np
sales = np.array([[200, 250, 300, 400],
[150, 300, 200, 350],
[100, 200, 250, 300]])
# Sum sales per shop (row-wise)
sales_per_shop = np.sum(sales, axis=1)
print(“Total sales per shop:”, sales_per_shop)
# Sum sales per month (column-wise)
sales_per_month = np.sum(sales, axis=0)
print(“Total sales per month:”, sales_per_month)
Output:
Total sales per shop: [1150 1000 850]
Total sales per month: [ 450 750 750 1050]
-
Pandas
Pandas is a robust Python toolkit for data manipulation and analysis, offering simple data structures such as DataFrames. It enables you to efficiently handle, clean, and analyze huge datasets, much like dealing with rows and columns in Excel.
Pandas is a strong Python toolkit for data manipulation and analysis, offering simple data structures such as DataFrames. It enables you to efficiently handle, clean, and analyze huge datasets, much like dealing with rows and columns in Excel.
With Pandas, you can filter, aggregate, and visualize data effortlessly, making it an essential tool for data science tasks.
Example:
-
Grouping Data: Summarizing Sales by Product
You can group data by categories (like products) and then calculate the total sales for each product.
Before running, simply give pip install pandas
import pandas as pd
# Creating a sales DataFrame
data = {‘Product’: [‘Dosa’, ‘Idli’, ‘Dosa’, ‘Vada’, ‘Idli’],
‘Sales’: [100, 150, 80, 90, 120]}
df = pd.DataFrame(data)
# Grouping data by ‘Product’ and summing the ‘Sales’
grouped_sales = df.groupby(‘Product’)[‘Sales’].sum()
print(grouped_sales)
Output:
Product
Dosa 180
Idli 270
Vada 90
Name: Sales, dtype: int64
Filling in Missing Values
You can fill in the missing data with a default value or the mean of the column.
import pandas as pd
# Creating a DataFrame with missing values
data = {‘Name’: [‘Anil’, ‘Bala’, ‘Chetan’, ‘Divya’],
‘Age’: [25, None, 22, 29],
‘City’: [‘Chennai’, ‘Hyderabad’, None, ‘Mumbai’]}
df = pd.DataFrame(data)
# Filling missing age with the mean age and missing city with a default value
df[‘Age’].fillna(df[‘Age’].mean(), inplace=True)
df[‘City’].fillna(‘Unknown’, inplace=True)
print(df)
Output:
Name Age City
0 Anil 25.000000 Chennai
1 Bala 25.333333 Hyderabad
2 Chetan 22.000000 Unknown
3 Divya 29.000000 Mumbai
-
Matplotlib
Matplotlib is a popular Python library for creating static, interactive, and animated visualizations. It allows you to generate plots, graphs, and charts to visualize data in a simple and customizable way.
With just a few lines of code, you can create line plots, bar charts, histograms, and more to gain insights from data.
Matplotlib is like drawing a beautiful kolam (rangoli) in front of your home or restaurant, showcasing information visually. It lets you create colorful graphs and charts to understand trends.
Example
-
Visualizing the number of dosas sold in a week.
Before running, simply give pip install Matplotlib
import matplotlib.pyplot as plt
dosa_sales = [50, 60, 80, 90, 70]
plt.plot(dosa_sales)
plt.title(‘Dosa Sales Over the Week’)
plt.show()
Output:
Pie Chart: Market Share
Displaying market share of different companies in a pie chart.
import matplotlib.pyplot as plt
# Data for companies and market share
companies = [‘Greenbees’, ‘Mahindra’, ‘Techpark’, ‘Orangewar’]
market_share = [400, 256, 200, 159]
# Creating a pie chart
plt.pie(market_share, labels=companies, autopct=’%1.1f%%’, startangle=90)
plt.title(“Market Share Distribution”)
plt.show()
-
BeautifulSoup
BeautifulSoup (part of the BeautifulSoup4 package) is widely used for web scraping. It allows you to effortlessly extract data from HTML and XML texts.
It’s like an expert detective digging through jumbled web pages to find crucial information hidden within. It allows you to search through the HTML structure of webpages to locate exactly what you’re looking for, much like a detective collecting clues.
Example:
Extracting all the headlines from a news website.
Before running, simply give pip install Beautifulsoup
from bs4 import BeautifulSoup
import requests
url = ‘https://newswebsite.com’
response = requests.get(url)
soup = BeautifulSoup(response.text, ‘html.parser’)
# Extracting all the headlines (h2 tags)
headlines = soup.find_all(‘h2’)
for headline in headlines:
print(headline.text)
Output:
How to Master Layering for Cold Weather
10 Eco-Friendly Accessories to Elevate Any Outfit
Fashion Trends of 2024: What’s In and What’s Out
Top Adventure Tourism Ideas for Your Next Trip
20 Healthy Snacks That Actually Taste Good: How to Satisfy Your Cravings Without Guilt
-
Scikit-learn