trăn gấu trúc
- Tram Ho
Panda là một thư viện để thao tác với dữ liệu bằng Python. Thông tin nhanh: | Sự thật | Mô tả | | ——– | ——– | | Trang chủ | https://pandas.pydata.org | | API doc | https://pandas.pydata.org/docs/reference/index.html | | Năm đầu tiên | Ngày 05 tháng 8 năm 2009 (13 năm trước). https://github.com/pandas-dev/pandas/commit/ec1a0a2a2 | | Mã nguồn | https://github.com/pandas-dev/pandas | | Thẻ Stack Overflow | https://stackoverflow.com/questions/tagged/pandas | | Phiên bản ổn định mới nhất | 1.4.2 '(02 tháng 4, 2022) | Môi trường phát triển! [Image.png] (https://images.viblo.asia/d55ee195-ae53-4879-8d59-4445ab5ea71a.png) Install pandas! [Install_panda] (https://user-images.githubusercontent.com/ 1328316 / 164500646-3330a0bb-7eea-433a-aa60-c03d90b75493.png) Phiên bản Python
'(pythonProject1) C: Users donhu> python --version Python 3.10.0
Cài đặt! [Image.png] (https://images.viblo.asia/f65d8acc-6bd5-4231-9e1c-6e91fc01ec6a.png)! [image.png] (https://images.viblo.asia/71d14c2d-3c9e-453c-a60d-51c4bb6cfa2e. png) Thuộc tính và phương pháp với đối tượng gấu trúc
python import pandas as pd df = pd.DataFrame ({"Name": ["Braund, Mr. Owen Harris", "Allen, Mr. William Henry", "Bonnell, Miss . Elizabeth ",]," Tuổi ": [22, 35, 58]," Giới tính ": [" nam "," nam "," nữ "],}) print (" n01 ------- ---------- ") print (df) print () print (" n02 ----------------- ") print (df [" Tuổi " ]) print (" n03 -----------------") age = pd.Series ([22, 35, 58], name = "Age") print (age) print (" n04 -----------------") print (df ["Tuổi"]. max ()) print (" n05 ------- ---------- ") print (age.max ()) print (" n06 ----------------- ") print (df.describe ( )) # https://raw.githubusercontent.com/pandas-dev/pandas/main/doc/data/titanic.csv print (" n07 ----------------- ") titanic = pd.read_csv (" vy / titanic.csv ") print (titanic) print (" n08 ----------------- ") print (titanic.head ( 2)) print (" n09 -----------------") print (titanic.dtypes) print (" n10 ------------ ----- ") # pip install openpyxl # conda install openpyxl print (titanic.to_excel (" minh_thu.xlsx ", sheet_name =" loves ", index = False)) print (" n11 ------- ---------- ") my_titanic = pd.read_excel (" minh_thu.xlsx ", sheet_name =" loves ") print (my_titanic.head (3)) print (" n12 ------ ----------- ") print (my_titanic.info ()) # https://raw.githubusercontent.com/pandas-dev/pandas/main/pandas/tests/io/data/csv/ tips.csv url = ("https://raw.github.com/pandas-dev" "/pandas/main/pandas/tests/io/data/csv/tips.csv") tips = pd.read_csv (url) print (" n12b -----------------") print (mẹo) print (" n14 ----------------- ") sorted_df = tips.sort_val ues (by = 'total_bill') print (sorted_df) print (" n15 -----------------") sorted_df = tips.sort_values (by = 'total_bill', ascending = Sai) print (sorted_df)
result '' C: ProgramData Anaconda3 envs pythonProject1 python.exe C: /Users/donhu/PycharmProjects/pythonProject1/vy_panda_01.py 01 -------- --------- Tên Tuổi Giới tính 0 Braund, Ông Owen Harris 22 nam 1 Allen, Ông William Henry 35 nam 2 Bonnell, Cô Elizabeth 58 nữ 02 ----------- ------ 0 22 1 35 2 58 Tên: Tuổi, kiểu: int64 03 ----------------- 0 22 1 35 2 58 Tên: Tuổi, kiểu: int64 04 ----------------- 58 05 ----------------- 58 06 ----------- ------ Đếm tuổi 3,000000 trung bình 38.333333 std 18.230012 min 22.000000 25% 28.500000 50% 35.000000 75% 46.500000 max 58.000000 07 ----------------- PassengerId Survived Pclass ... Giá vé Cabin đã bắt đầu 0 1 0 3 ... 7.2500 NaN S 1 2 1 1 ... 71.2833 C85 C 2 3 1 3 ... 7.9250 NaN S 3 4 1 1 ... 53.1000 C123 S 4 5 0 3 ... 8.0500 NaN S .. ... ... ... ... ... ... ... 886 887 0 2 ... 13.0000 NaN S 887 888 1 1 ... 3 0,0000 B42 S 888 889 0 3 ... 23,4500 NaN S 889 890 1 1 ... 30,0000 C148 C 890 891 0 3 ... 7,7500 NaN Q [891 hàng x 12 cột] 08 --------- -------- PassengerId Survived Pclass ... Fare Cabin Embarked 0 1 0 3 ... 7.2500 NaN S 1 2 1 1 ... 71.2833 C85 C [2 hàng x 12 cột] 09 ----- ------------ PassengerId int64 Tồn tại int64 Lớp int64 Tên đối tượng Đối tượng giới tính Tuổi float64 SibSp int64 Parch int64 Đối tượng vé Giá vé float64 Đối tượng cabin Đối tượng nhúng: đối tượng 10 ---------- ------- Không có 11 ----------------- Hạng hành khách còn sống ... Buồng vé đã bắt đầu 0 1 0 3 ... 7.2500 NaN S 1 2 1 1. .. 71.2833 C85 C 2 3 1 3 ... 7.9250 NaN S [3 hàng x 12 cột] 12 -----------------
! [image.png] (https://images.viblo.asia/47607cba-5549-43de-a340-f8243546c4cc.png) ** Pandas Excel API ** Cần cài đặt pandas
và openpyxl
bên trong Miniconda trước khi thực hành. Đây là ** đọc excel ** hàm.
python import pandas as pd found_url = (" https://m.hvtc.edu.vn/Portals/0/01_2018/01.DS%20TN_9.2021%20.xlsx ") hehe = pd.read_excel (found_url ) hehe
Kết quả! [image] (https://user-images.githubusercontent.com/1328316/164501346-f0299556-9298-4181-8a5f-e53f26b73560.png) Không có tiêu đề '' 'python hihi = pd.read_excel (found_url, index_col = None, header = None) hihi
rb
có nghĩa là r
+ b
= read + binary. Xem https://docs.python.org/3/library/functions.html#open python hoho = pd.read_excel (open ('C: \ Users \ donhu \ Desktop \ 01.DS TN_9 .2021 .xlsx ',' rb '), sheet_name =' LC22 ') hoho' '
Nguồn bài viết : Viblo