Pandas 是 Python 中最流行的數(shù)據(jù)操作和分析庫之一。它提供了一個(gè)強(qiáng)大的數(shù)據(jù)結(jié)構(gòu),稱為 DataFrame,它允許你輕松存儲(chǔ)和操作結(jié)構(gòu)化數(shù)據(jù)。
import pandas as pd# Create a DataFramedata = {'Name': ['Alice', 'Bob', 'Charlie'], 'Age': [25, 30, 35], 'Occupation': ['Engineer', 'Teacher', 'Designer']}df = pd.DataFrame(data)print(df)
NumPy 是 Python 中科學(xué)計(jì)算的基礎(chǔ)庫。它提供對大型多維數(shù)組和矩陣的支持,以及對這些數(shù)組進(jìn)行操作的數(shù)學(xué)函數(shù)集合。
arr = np.array([[1, 2, 3], [4, 5, 6]])print(arr)
Matplotlib 是一個(gè)繪圖庫,允許你創(chuàng)建各種類型的繪圖,包括線圖、條形圖、直方圖和散點(diǎn)圖。
import matplotlib.pyplot as plt# Create a line plotx = [1, 2, 3, 4, 5]y = [1, 4, 9, 16, 25]plt.plot(x, y)plt.show()
Requests
Requests 是一個(gè)用于在 Python 中發(fā)出 HTTP 請求的庫。它簡化了發(fā)送 HTTP 請求和處理響應(yīng)的過程。
import requests# Send a GET requestresponse = requests.get('https://www.example.com')print(response.text)
BeautifulSoup 是一個(gè)用于解析 HTML 和 XML 文檔的庫。它可以輕松地從網(wǎng)頁中提取數(shù)據(jù)并導(dǎo)航文檔樹結(jié)構(gòu)。
from bs4 import BeautifulSoup# Parse an HTML documenthtml = '<html><body><h1>Example</h1></body></html>'soup = BeautifulSoup(html, 'html.parser')print(soup.h1.text)
SQLAlchemy 是 Python 的對象關(guān)系映射 (ORM) 庫。它提供了一種使用 Python 對象與數(shù)據(jù)庫交互的方式,使得管理數(shù)據(jù)庫操作變得更加容易。
from sqlalchemy import create_engine, Column, Integer, Stringfrom sqlalchemy.ext.declarative import declarative_basefrom sqlalchemy.orm import sessionmaker# Define a database modelBase = declarative_base()class User(Base): __tablename__ = 'users' id = Column(Integer, primary_key=True) name = Column(String)# Create a database sessionengine = create_engine('sqlite:///example.db')Session = sessionmaker(bind=engine)session = Session()# Add a new useruser = User(name='Alice')session.add(user)session.commit()# Query the users tableusers = session.query(User).all()for user in users: print(user.name)
Scikit-learn 是 Python 中的機(jī)器學(xué)習(xí)庫。它提供了一系列用于數(shù)據(jù)挖掘、數(shù)據(jù)分析和預(yù)測建模的算法和工具。
from sklearn.ensemble import RandomForestClassifierfrom sklearn.datasets import load_iris# Load the Iris datasetdata = load_iris()# Train a random forest classifierclassifier = RandomForestClassifier()classifier.fit(data.data, data.target)# Make predictionspredictions = classifier.predict([[5.1, 3.5, 1.4, 0.2], [6.2, 2.9, 4.3, 1.3]])print(predictions)
TensorFlow 是一個(gè)用于數(shù)值計(jì)算和機(jī)器學(xué)習(xí)的庫。它為構(gòu)建和訓(xùn)練各種類型的機(jī)器學(xué)習(xí)模型提供了靈活的框架。
import tensorflow as tf# Create a TensorFlow constanta = tf.constant(1)b = tf.constant(2)# Perform a computationc = tf.add(a, b)# Run the computationwith tf.Session() as sess: result = sess.run(c) print(result)
Django 是 Python 的高級 Web 框架。它提供了一種干凈、高效的方式來構(gòu)建 Web 應(yīng)用程序、處理 URL 路由、數(shù)據(jù)庫管理和表單處理等任務(wù)。
from django.urls import pathfrom django.http import HttpResponse# Define a viewdef hello(request): return HttpResponse('Hello, World!')# Define URLsurlpatterns = [ path('hello/', hello),]# Configure and run the Django applicationfrom django.core.wsgi import get_wsgi_applicationapplication = get_wsgi_application()
Pytest 是 Python 的測試框架。它簡化了編寫測試的過程,并提供了強(qiáng)大的功能,例如測試發(fā)現(xiàn)、測試參數(shù)化和固定裝置。
import pytest# Define a test functiondef test_addition(): result = 1 + 2 assert result == 3# Run the testspytest.main()
本文鏈接:http://www.www897cc.com/showinfo-26-70390-0.html十個(gè)超有用的 Python 的庫
聲明:本網(wǎng)頁內(nèi)容旨在傳播知識(shí),若有侵權(quán)等問題請及時(shí)與本網(wǎng)聯(lián)系,我們將在第一時(shí)間刪除處理。郵件:2376512515@qq.com