Tôi muốn tạo một bảng với thông tin có sẵn trên trang web này. Tôi muốn bảng có 3 cột: 0 Series/Date, 1 tiêu đề và 2 liên kết. Tôi đã quản lý để có được hai cột đầu tiên nhưng tôi không biết làm thế nào để có được liên kết cho mỗi mục.
import pandas as pd
import requests
url = "//legislaturautuado.com/pgs/resolutions.php?st=5&f=2016"
r = requests.get[url]
df_list = pd.read_html[r.text]
df = df_list[0]
df.head[]
Sẽ có thể có được những gì tôi muốn bằng cách chỉ sử dụng gấu trúc?
Đã hỏi ngày 27 tháng 11 năm 2020 lúc 18:32Nov 27, 2020 at 18:32
1
Theo như tôi biết, không thể với gấu trúc. Nó có thể được thực hiện với
In [13]: df.head[] Out[13]: A B C D 2013-01-01 0.469112 -0.282863 -1.509059 -1.135632 2013-01-02 1.212112 -0.173215 0.119209 -1.044236 2013-01-03 -0.861849 -2.104569 -0.494929 1.071804 2013-01-04 0.721555 -0.706771 -1.039575 0.271860 2013-01-05 -0.424972 0.567020 0.276232 -1.087401 In [14]: df.tail[3] Out[14]: A B C D 2013-01-04 0.721555 -0.706771 -1.039575 0.271860 2013-01-05 -0.424972 0.567020 0.276232 -1.087401 2013-01-06 -0.673690 0.113648 -1.478427 0.5249882, mặc dù:
import requests
import pandas as pd
from bs4 import BeautifulSoup
url = "//legislaturautuado.com/pgs/resolutions.php?st=5&f=2016"
r = requests.get[url]
html_table = BeautifulSoup[r.text].find['table']
r.close[]
df = pd.read_html[str[html_table], header=0][0]
df['Link'] = [link.get['href'] for link in html_table.find_all['a']]
Đã trả lời ngày 27 tháng 11 năm 2020 lúc 19:57Nov 27, 2020 at 19:57
3
Đây là một giới thiệu ngắn về gấu trúc, chủ yếu dành cho người dùng mới. Bạn có thể thấy các công thức nấu ăn phức tạp hơn trong sách nấu ăn.Cookbook.
Theo thông thường, chúng tôi nhập như sau:
In [1]: import numpy as np In [2]: import pandas as pd
Tạo đối tượng#
Xem phần Giới thiệu về Cấu trúc dữ liệu.Intro to data structures section.
Tạo
In [13]: df.head[] Out[13]: A B C D 2013-01-01 0.469112 -0.282863 -1.509059 -1.135632 2013-01-02 1.212112 -0.173215 0.119209 -1.044236 2013-01-03 -0.861849 -2.104569 -0.494929 1.071804 2013-01-04 0.721555 -0.706771 -1.039575 0.271860 2013-01-05 -0.424972 0.567020 0.276232 -1.087401 In [14]: df.tail[3] Out[14]: A B C D 2013-01-04 0.721555 -0.706771 -1.039575 0.271860 2013-01-05 -0.424972 0.567020 0.276232 -1.087401 2013-01-06 -0.673690 0.113648 -1.478427 0.5249883 bằng cách chuyển danh sách các giá trị, để gấu trúc tạo chỉ mục số nguyên mặc định:
In [3]: s = pd.Series[[1, 3, 5, np.nan, 6, 8]] In [4]: s Out[4]: 0 1.0 1 3.0 2 5.0 3 NaN 4 6.0 5 8.0 dtype: float64
Tạo một
In [13]: df.head[] Out[13]: A B C D 2013-01-01 0.469112 -0.282863 -1.509059 -1.135632 2013-01-02 1.212112 -0.173215 0.119209 -1.044236 2013-01-03 -0.861849 -2.104569 -0.494929 1.071804 2013-01-04 0.721555 -0.706771 -1.039575 0.271860 2013-01-05 -0.424972 0.567020 0.276232 -1.087401 In [14]: df.tail[3] Out[14]: A B C D 2013-01-04 0.721555 -0.706771 -1.039575 0.271860 2013-01-05 -0.424972 0.567020 0.276232 -1.087401 2013-01-06 -0.673690 0.113648 -1.478427 0.5249884 bằng cách chuyển một mảng numpy, với một chỉ mục DateTime bằng cách sử dụng các cột được dán nhãn và được dán nhãn:
In [5]: dates = pd.date_range["20130101", periods=6] In [6]: dates Out[6]: DatetimeIndex[['2013-01-01', '2013-01-02', '2013-01-03', '2013-01-04', '2013-01-05', '2013-01-06'], dtype='datetime64[ns]', freq='D'] In [7]: df = pd.DataFrame[np.random.randn[6, 4], index=dates, columns=list["ABCD"]] In [8]: df Out[8]: A B C D 2013-01-01 0.469112 -0.282863 -1.509059 -1.135632 2013-01-02 1.212112 -0.173215 0.119209 -1.044236 2013-01-03 -0.861849 -2.104569 -0.494929 1.071804 2013-01-04 0.721555 -0.706771 -1.039575 0.271860 2013-01-05 -0.424972 0.567020 0.276232 -1.087401 2013-01-06 -0.673690 0.113648 -1.478427 0.524988
Tạo
In [13]: df.head[] Out[13]: A B C D 2013-01-01 0.469112 -0.282863 -1.509059 -1.135632 2013-01-02 1.212112 -0.173215 0.119209 -1.044236 2013-01-03 -0.861849 -2.104569 -0.494929 1.071804 2013-01-04 0.721555 -0.706771 -1.039575 0.271860 2013-01-05 -0.424972 0.567020 0.276232 -1.087401 In [14]: df.tail[3] Out[14]: A B C D 2013-01-04 0.721555 -0.706771 -1.039575 0.271860 2013-01-05 -0.424972 0.567020 0.276232 -1.087401 2013-01-06 -0.673690 0.113648 -1.478427 0.5249884 bằng cách truyền từ điển các đối tượng có thể được chuyển đổi thành một cấu trúc giống như loạt:
In [9]: df2 = pd.DataFrame[ ...: { ...: "A": 1.0, ...: "B": pd.Timestamp["20130102"], ...: "C": pd.Series[1, index=list[range[4]], dtype="float32"], ...: "D": np.array[[3] * 4, dtype="int32"], ...: "E": pd.Categorical[["test", "train", "test", "train"]], ...: "F": "foo", ...: } ...: ] ...: In [10]: df2 Out[10]: A B C D E F 0 1.0 2013-01-02 1.0 3 test foo 1 1.0 2013-01-02 1.0 3 train foo 2 1.0 2013-01-02 1.0 3 test foo 3 1.0 2013-01-02 1.0 3 train foo
Các cột của kết quả
In [13]: df.head[] Out[13]: A B C D 2013-01-01 0.469112 -0.282863 -1.509059 -1.135632 2013-01-02 1.212112 -0.173215 0.119209 -1.044236 2013-01-03 -0.861849 -2.104569 -0.494929 1.071804 2013-01-04 0.721555 -0.706771 -1.039575 0.271860 2013-01-05 -0.424972 0.567020 0.276232 -1.087401 In [14]: df.tail[3] Out[14]: A B C D 2013-01-04 0.721555 -0.706771 -1.039575 0.271860 2013-01-05 -0.424972 0.567020 0.276232 -1.087401 2013-01-06 -0.673690 0.113648 -1.478427 0.5249884 có các DTYPE khác nhau:dtypes:
In [11]: df2.dtypes Out[11]: A float64 B datetime64[ns] C float32 D int32 E category F object dtype: object
Nếu bạn sử dụng Ipython, Tab hoàn thành cho tên cột [cũng như các thuộc tính công khai] sẽ tự động được bật. Ở đây, một tập hợp con của các thuộc tính sẽ được hoàn thành:
In [12]: df2. # noqa: E225, E999 df2.A df2.bool df2.abs df2.boxplot df2.add df2.C df2.add_prefix df2.clip df2.add_suffix df2.columns df2.align df2.copy df2.all df2.count df2.any df2.combine df2.append df2.D df2.apply df2.describe df2.applymap df2.diff df2.B df2.duplicated
Như bạn có thể thấy, các cột
In [13]: df.head[] Out[13]: A B C D 2013-01-01 0.469112 -0.282863 -1.509059 -1.135632 2013-01-02 1.212112 -0.173215 0.119209 -1.044236 2013-01-03 -0.861849 -2.104569 -0.494929 1.071804 2013-01-04 0.721555 -0.706771 -1.039575 0.271860 2013-01-05 -0.424972 0.567020 0.276232 -1.087401 In [14]: df.tail[3] Out[14]: A B C D 2013-01-04 0.721555 -0.706771 -1.039575 0.271860 2013-01-05 -0.424972 0.567020 0.276232 -1.087401 2013-01-06 -0.673690 0.113648 -1.478427 0.5249888,
In [13]: df.head[] Out[13]: A B C D 2013-01-01 0.469112 -0.282863 -1.509059 -1.135632 2013-01-02 1.212112 -0.173215 0.119209 -1.044236 2013-01-03 -0.861849 -2.104569 -0.494929 1.071804 2013-01-04 0.721555 -0.706771 -1.039575 0.271860 2013-01-05 -0.424972 0.567020 0.276232 -1.087401 In [14]: df.tail[3] Out[14]: A B C D 2013-01-04 0.721555 -0.706771 -1.039575 0.271860 2013-01-05 -0.424972 0.567020 0.276232 -1.087401 2013-01-06 -0.673690 0.113648 -1.478427 0.5249889,
In [15]: df.index Out[15]: DatetimeIndex[['2013-01-01', '2013-01-02', '2013-01-03', '2013-01-04', '2013-01-05', '2013-01-06'], dtype='datetime64[ns]', freq='D'] In [16]: df.columns Out[16]: Index[['A', 'B', 'C', 'D'], dtype='object']0 và
In [15]: df.index Out[15]: DatetimeIndex[['2013-01-01', '2013-01-02', '2013-01-03', '2013-01-04', '2013-01-05', '2013-01-06'], dtype='datetime64[ns]', freq='D'] In [16]: df.columns Out[16]: Index[['A', 'B', 'C', 'D'], dtype='object']1 được tự động hoàn thành.
In [15]: df.index Out[15]: DatetimeIndex[['2013-01-01', '2013-01-02', '2013-01-03', '2013-01-04', '2013-01-05', '2013-01-06'], dtype='datetime64[ns]', freq='D'] In [16]: df.columns Out[16]: Index[['A', 'B', 'C', 'D'], dtype='object']2 và
In [15]: df.index Out[15]: DatetimeIndex[['2013-01-01', '2013-01-02', '2013-01-03', '2013-01-04', '2013-01-05', '2013-01-06'], dtype='datetime64[ns]', freq='D'] In [16]: df.columns Out[16]: Index[['A', 'B', 'C', 'D'], dtype='object']3 cũng có; Phần còn lại của các thuộc tính đã bị cắt ngắn cho sự ngắn gọn.
Xem dữ liệu#
Xem phần cơ bản.Basics section.
Sử dụng
In [15]: df.index Out[15]: DatetimeIndex[['2013-01-01', '2013-01-02', '2013-01-03', '2013-01-04', '2013-01-05', '2013-01-06'], dtype='datetime64[ns]', freq='D'] In [16]: df.columns Out[16]: Index[['A', 'B', 'C', 'D'], dtype='object']4 và
In [15]: df.index Out[15]: DatetimeIndex[['2013-01-01', '2013-01-02', '2013-01-03', '2013-01-04', '2013-01-05', '2013-01-06'], dtype='datetime64[ns]', freq='D'] In [16]: df.columns Out[16]: Index[['A', 'B', 'C', 'D'], dtype='object']5 để xem các hàng trên và dưới của khung tương ứng:
In [13]: df.head[] Out[13]: A B C D 2013-01-01 0.469112 -0.282863 -1.509059 -1.135632 2013-01-02 1.212112 -0.173215 0.119209 -1.044236 2013-01-03 -0.861849 -2.104569 -0.494929 1.071804 2013-01-04 0.721555 -0.706771 -1.039575 0.271860 2013-01-05 -0.424972 0.567020 0.276232 -1.087401 In [14]: df.tail[3] Out[14]: A B C D 2013-01-04 0.721555 -0.706771 -1.039575 0.271860 2013-01-05 -0.424972 0.567020 0.276232 -1.087401 2013-01-06 -0.673690 0.113648 -1.478427 0.524988
Hiển thị
In [15]: df.index Out[15]: DatetimeIndex[['2013-01-01', '2013-01-02', '2013-01-03', '2013-01-04', '2013-01-05', '2013-01-06'], dtype='datetime64[ns]', freq='D'] In [16]: df.columns Out[16]: Index[['A', 'B', 'C', 'D'], dtype='object']6 hoặc
In [15]: df.index Out[15]: DatetimeIndex[['2013-01-01', '2013-01-02', '2013-01-03', '2013-01-04', '2013-01-05', '2013-01-06'], dtype='datetime64[ns]', freq='D'] In [16]: df.columns Out[16]: Index[['A', 'B', 'C', 'D'], dtype='object']7:
In [15]: df.index Out[15]: DatetimeIndex[['2013-01-01', '2013-01-02', '2013-01-03', '2013-01-04', '2013-01-05', '2013-01-06'], dtype='datetime64[ns]', freq='D'] In [16]: df.columns Out[16]: Index[['A', 'B', 'C', 'D'], dtype='object']
In [15]: df.index Out[15]: DatetimeIndex[['2013-01-01', '2013-01-02', '2013-01-03', '2013-01-04', '2013-01-05', '2013-01-06'], dtype='datetime64[ns]', freq='D'] In [16]: df.columns Out[16]: Index[['A', 'B', 'C', 'D'], dtype='object']8 đưa ra một biểu diễn nhảm nhí của dữ liệu cơ bản. Lưu ý rằng đây có thể là một hoạt động đắt tiền khi
In [13]: df.head[] Out[13]: A B C D 2013-01-01 0.469112 -0.282863 -1.509059 -1.135632 2013-01-02 1.212112 -0.173215 0.119209 -1.044236 2013-01-03 -0.861849 -2.104569 -0.494929 1.071804 2013-01-04 0.721555 -0.706771 -1.039575 0.271860 2013-01-05 -0.424972 0.567020 0.276232 -1.087401 In [14]: df.tail[3] Out[14]: A B C D 2013-01-04 0.721555 -0.706771 -1.039575 0.271860 2013-01-05 -0.424972 0.567020 0.276232 -1.087401 2013-01-06 -0.673690 0.113648 -1.478427 0.5249884 của bạn có các cột có các loại dữ liệu khác nhau, đi kèm với sự khác biệt cơ bản giữa gấu trúc và numpy: mảng numpy có một dtype cho toàn bộ mảng, trong khi các đoạn dữ liệu gấu trúc có một dtype trên mỗi cột. Khi bạn gọi
In [15]: df.index Out[15]: DatetimeIndex[['2013-01-01', '2013-01-02', '2013-01-03', '2013-01-04', '2013-01-05', '2013-01-06'], dtype='datetime64[ns]', freq='D'] In [16]: df.columns Out[16]: Index[['A', 'B', 'C', 'D'], dtype='object']8, Pandas sẽ tìm thấy DTYPE numpy có thể chứa tất cả các DTYPE trong DataFrame. Điều này có thể kết thúc là
import requests
import pandas as pd
from bs4 import BeautifulSoup
url = "//legislaturautuado.com/pgs/resolutions.php?st=5&f=2016"
r = requests.get[url]
html_table = BeautifulSoup[r.text].find['table']
r.close[]
df = pd.read_html[str[html_table], header=0][0]
df['Link'] = [link.get['href'] for link in html_table.find_all['a']]
01, đòi hỏi phải sử dụng mọi giá trị cho một đối tượng Python.NumPy arrays have one dtype for the entire array, while pandas DataFrames have one dtype per column. When you call In [15]: df.index Out[15]: DatetimeIndex[['2013-01-01', '2013-01-02', '2013-01-03', '2013-01-04', '2013-01-05', '2013-01-06'], dtype='datetime64[ns]', freq='D'] In [16]: df.columns Out[16]: Index[['A', 'B', 'C', 'D'], dtype='object']8, pandas will find the NumPy dtype that can hold all of the dtypes in the DataFrame. This may end up being
import requests
import pandas as pd
from bs4 import BeautifulSoup
url = "//legislaturautuado.com/pgs/resolutions.php?st=5&f=2016"
r = requests.get[url]
html_table = BeautifulSoup[r.text].find['table']
r.close[]
df = pd.read_html[str[html_table], header=0][0]
df['Link'] = [link.get['href'] for link in html_table.find_all['a']]
01, which requires casting every
value to a Python object.Đối với
import requests
import pandas as pd
from bs4 import BeautifulSoup
url = "//legislaturautuado.com/pgs/resolutions.php?st=5&f=2016"
r = requests.get[url]
html_table = BeautifulSoup[r.text].find['table']
r.close[]
df = pd.read_html[str[html_table], header=0][0]
df['Link'] = [link.get['href'] for link in html_table.find_all['a']]
02, In [13]: df.head[] Out[13]: A B C D 2013-01-01 0.469112 -0.282863 -1.509059 -1.135632 2013-01-02 1.212112 -0.173215 0.119209 -1.044236 2013-01-03 -0.861849 -2.104569 -0.494929 1.071804 2013-01-04 0.721555 -0.706771 -1.039575 0.271860 2013-01-05 -0.424972 0.567020 0.276232 -1.087401 In [14]: df.tail[3] Out[14]: A B C D 2013-01-04 0.721555 -0.706771 -1.039575 0.271860 2013-01-05 -0.424972 0.567020 0.276232 -1.087401 2013-01-06 -0.673690 0.113648 -1.478427 0.5249884 của chúng tôi trong tất cả các giá trị dấu phẩy động và
In [15]: df.index Out[15]: DatetimeIndex[['2013-01-01', '2013-01-02', '2013-01-03', '2013-01-04', '2013-01-05', '2013-01-06'], dtype='datetime64[ns]', freq='D'] In [16]: df.columns Out[16]: Index[['A', 'B', 'C', 'D'], dtype='object']8 là nhanh và không yêu cầu sao chép dữ liệu:
import requests
import pandas as pd
from bs4 import BeautifulSoup
url = "//legislaturautuado.com/pgs/resolutions.php?st=5&f=2016"
r = requests.get[url]
html_table = BeautifulSoup[r.text].find['table']
r.close[]
df = pd.read_html[str[html_table], header=0][0]
df['Link'] = [link.get['href'] for link in html_table.find_all['a']]
0Đối với
import requests
import pandas as pd
from bs4 import BeautifulSoup
url = "//legislaturautuado.com/pgs/resolutions.php?st=5&f=2016"
r = requests.get[url]
html_table = BeautifulSoup[r.text].find['table']
r.close[]
df = pd.read_html[str[html_table], header=0][0]
df['Link'] = [link.get['href'] for link in html_table.find_all['a']]
05, In [13]: df.head[] Out[13]: A B C D 2013-01-01 0.469112 -0.282863 -1.509059 -1.135632 2013-01-02 1.212112 -0.173215 0.119209 -1.044236 2013-01-03 -0.861849 -2.104569 -0.494929 1.071804 2013-01-04 0.721555 -0.706771 -1.039575 0.271860 2013-01-05 -0.424972 0.567020 0.276232 -1.087401 In [14]: df.tail[3] Out[14]: A B C D 2013-01-04 0.721555 -0.706771 -1.039575 0.271860 2013-01-05 -0.424972 0.567020 0.276232 -1.087401 2013-01-06 -0.673690 0.113648 -1.478427 0.5249884 với nhiều DTYPE,
In [15]: df.index Out[15]: DatetimeIndex[['2013-01-01', '2013-01-02', '2013-01-03', '2013-01-04', '2013-01-05', '2013-01-06'], dtype='datetime64[ns]', freq='D'] In [16]: df.columns Out[16]: Index[['A', 'B', 'C', 'D'], dtype='object']8 tương đối đắt tiền:
import requests
import pandas as pd
from bs4 import BeautifulSoup
url = "//legislaturautuado.com/pgs/resolutions.php?st=5&f=2016"
r = requests.get[url]
html_table = BeautifulSoup[r.text].find['table']
r.close[]
df = pd.read_html[str[html_table], header=0][0]
df['Link'] = [link.get['href'] for link in html_table.find_all['a']]
1import requests
import pandas as pd
from bs4 import BeautifulSoup
url = "//legislaturautuado.com/pgs/resolutions.php?st=5&f=2016"
r = requests.get[url]
html_table = BeautifulSoup[r.text].find['table']
r.close[]
df = pd.read_html[str[html_table], header=0][0]
df['Link'] = [link.get['href'] for link in html_table.find_all['a']]
08 hiển thị tóm tắt thống kê nhanh chóng về dữ liệu của bạn:import requests
import pandas as pd
from bs4 import BeautifulSoup
url = "//legislaturautuado.com/pgs/resolutions.php?st=5&f=2016"
r = requests.get[url]
html_table = BeautifulSoup[r.text].find['table']
r.close[]
df = pd.read_html[str[html_table], header=0][0]
df['Link'] = [link.get['href'] for link in html_table.find_all['a']]
2Chuyển dữ liệu của bạn:
import requests
import pandas as pd
from bs4 import BeautifulSoup
url = "//legislaturautuado.com/pgs/resolutions.php?st=5&f=2016"
r = requests.get[url]
html_table = BeautifulSoup[r.text].find['table']
r.close[]
df = pd.read_html[str[html_table], header=0][0]
df['Link'] = [link.get['href'] for link in html_table.find_all['a']]
3import requests
import pandas as pd
from bs4 import BeautifulSoup
url = "//legislaturautuado.com/pgs/resolutions.php?st=5&f=2016"
r = requests.get[url]
html_table = BeautifulSoup[r.text].find['table']
r.close[]
df = pd.read_html[str[html_table], header=0][0]
df['Link'] = [link.get['href'] for link in html_table.find_all['a']]
09 sắp xếp theo một trục:import requests
import pandas as pd
from bs4 import BeautifulSoup
url = "//legislaturautuado.com/pgs/resolutions.php?st=5&f=2016"
r = requests.get[url]
html_table = BeautifulSoup[r.text].find['table']
r.close[]
df = pd.read_html[str[html_table], header=0][0]
df['Link'] = [link.get['href'] for link in html_table.find_all['a']]
4import requests
import pandas as pd
from bs4 import BeautifulSoup
url = "//legislaturautuado.com/pgs/resolutions.php?st=5&f=2016"
r = requests.get[url]
html_table = BeautifulSoup[r.text].find['table']
r.close[]
df = pd.read_html[str[html_table], header=0][0]
df['Link'] = [link.get['href'] for link in html_table.find_all['a']]
10 Sắp xếp theo các giá trị:import requests
import pandas as pd
from bs4 import BeautifulSoup
url = "//legislaturautuado.com/pgs/resolutions.php?st=5&f=2016"
r = requests.get[url]
html_table = BeautifulSoup[r.text].find['table']
r.close[]
df = pd.read_html[str[html_table], header=0][0]
df['Link'] = [link.get['href'] for link in html_table.find_all['a']]
5Selection#
Xem lập chỉ mục lập chỉ mục và chọn dữ liệu và lập chỉ mục đa dạng / nâng cao.Indexing and Selecting Data and MultiIndex / Advanced Indexing.
Getting#
Chọn một cột duy nhất, mang lại
In [13]: df.head[] Out[13]: A B C D 2013-01-01 0.469112 -0.282863 -1.509059 -1.135632 2013-01-02 1.212112 -0.173215 0.119209 -1.044236 2013-01-03 -0.861849 -2.104569 -0.494929 1.071804 2013-01-04 0.721555 -0.706771 -1.039575 0.271860 2013-01-05 -0.424972 0.567020 0.276232 -1.087401 In [14]: df.tail[3] Out[14]: A B C D 2013-01-04 0.721555 -0.706771 -1.039575 0.271860 2013-01-05 -0.424972 0.567020 0.276232 -1.087401 2013-01-06 -0.673690 0.113648 -1.478427 0.5249883, tương đương với
import requests
import pandas as pd
from bs4 import BeautifulSoup
url = "//legislaturautuado.com/pgs/resolutions.php?st=5&f=2016"
r = requests.get[url]
html_table = BeautifulSoup[r.text].find['table']
r.close[]
df = pd.read_html[str[html_table], header=0][0]
df['Link'] = [link.get['href'] for link in html_table.find_all['a']]
12:import requests
import pandas as pd
from bs4 import BeautifulSoup
url = "//legislaturautuado.com/pgs/resolutions.php?st=5&f=2016"
r = requests.get[url]
html_table = BeautifulSoup[r.text].find['table']
r.close[]
df = pd.read_html[str[html_table], header=0][0]
df['Link'] = [link.get['href'] for link in html_table.find_all['a']]
6Chọn thông qua
import requests
import pandas as pd
from bs4 import BeautifulSoup
url = "//legislaturautuado.com/pgs/resolutions.php?st=5&f=2016"
r = requests.get[url]
html_table = BeautifulSoup[r.text].find['table']
r.close[]
df = pd.read_html[str[html_table], header=0][0]
df['Link'] = [link.get['href'] for link in html_table.find_all['a']]
13 [import requests
import pandas as pd
from bs4 import BeautifulSoup
url = "//legislaturautuado.com/pgs/resolutions.php?st=5&f=2016"
r = requests.get[url]
html_table = BeautifulSoup[r.text].find['table']
r.close[]
df = pd.read_html[str[html_table], header=0][0]
df['Link'] = [link.get['href'] for link in html_table.find_all['a']]
14], trong đó cắt các hàng:import requests
import pandas as pd
from bs4 import BeautifulSoup
url = "//legislaturautuado.com/pgs/resolutions.php?st=5&f=2016"
r = requests.get[url]
html_table = BeautifulSoup[r.text].find['table']
r.close[]
df = pd.read_html[str[html_table], header=0][0]
df['Link'] = [link.get['href'] for link in html_table.find_all['a']]
7Lựa chọn theo nhãn#
Xem thêm trong lựa chọn bằng nhãn bằng cách sử dụng
import requests
import pandas as pd
from bs4 import BeautifulSoup
url = "//legislaturautuado.com/pgs/resolutions.php?st=5&f=2016"
r = requests.get[url]
html_table = BeautifulSoup[r.text].find['table']
r.close[]
df = pd.read_html[str[html_table], header=0][0]
df['Link'] = [link.get['href'] for link in html_table.find_all['a']]
15 hoặc import requests
import pandas as pd
from bs4 import BeautifulSoup
url = "//legislaturautuado.com/pgs/resolutions.php?st=5&f=2016"
r = requests.get[url]
html_table = BeautifulSoup[r.text].find['table']
r.close[]
df = pd.read_html[str[html_table], header=0][0]
df['Link'] = [link.get['href'] for link in html_table.find_all['a']]
16.Selection by Label using import requests
import pandas as pd
from bs4 import BeautifulSoup
url = "//legislaturautuado.com/pgs/resolutions.php?st=5&f=2016"
r = requests.get[url]
html_table = BeautifulSoup[r.text].find['table']
r.close[]
df = pd.read_html[str[html_table], header=0][0]
df['Link'] = [link.get['href'] for link in html_table.find_all['a']]
15 or import requests
import pandas as pd
from bs4 import BeautifulSoup
url = "//legislaturautuado.com/pgs/resolutions.php?st=5&f=2016"
r = requests.get[url]
html_table = BeautifulSoup[r.text].find['table']
r.close[]
df = pd.read_html[str[html_table], header=0][0]
df['Link'] = [link.get['href'] for link in html_table.find_all['a']]
16.Để có được một mặt cắt bằng cách sử dụng nhãn:
import requests
import pandas as pd
from bs4 import BeautifulSoup
url = "//legislaturautuado.com/pgs/resolutions.php?st=5&f=2016"
r = requests.get[url]
html_table = BeautifulSoup[r.text].find['table']
r.close[]
df = pd.read_html[str[html_table], header=0][0]
df['Link'] = [link.get['href'] for link in html_table.find_all['a']]
8Chọn trên một trục đa nhãn theo nhãn:
import requests
import pandas as pd
from bs4 import BeautifulSoup
url = "//legislaturautuado.com/pgs/resolutions.php?st=5&f=2016"
r = requests.get[url]
html_table = BeautifulSoup[r.text].find['table']
r.close[]
df = pd.read_html[str[html_table], header=0][0]
df['Link'] = [link.get['href'] for link in html_table.find_all['a']]
9Hiển thị việc cắt nhãn, cả hai điểm cuối được bao gồm:
In [1]: import numpy as np In [2]: import pandas as pd0
Giảm kích thước của đối tượng được trả về:
In [1]: import numpy as np In [2]: import pandas as pd1
Để nhận được giá trị vô hướng:
In [1]: import numpy as np In [2]: import pandas as pd2
Để có quyền truy cập nhanh vào vô hướng [tương đương với phương pháp trước]:
In [1]: import numpy as np In [2]: import pandas as pd3
Lựa chọn theo vị trí#
Xem thêm trong lựa chọn theo vị trí bằng cách sử dụng
import requests
import pandas as pd
from bs4 import BeautifulSoup
url = "//legislaturautuado.com/pgs/resolutions.php?st=5&f=2016"
r = requests.get[url]
html_table = BeautifulSoup[r.text].find['table']
r.close[]
df = pd.read_html[str[html_table], header=0][0]
df['Link'] = [link.get['href'] for link in html_table.find_all['a']]
17 hoặc import requests
import pandas as pd
from bs4 import BeautifulSoup
url = "//legislaturautuado.com/pgs/resolutions.php?st=5&f=2016"
r = requests.get[url]
html_table = BeautifulSoup[r.text].find['table']
r.close[]
df = pd.read_html[str[html_table], header=0][0]
df['Link'] = [link.get['href'] for link in html_table.find_all['a']]
16.Selection by Position using import requests
import pandas as pd
from bs4 import BeautifulSoup
url = "//legislaturautuado.com/pgs/resolutions.php?st=5&f=2016"
r = requests.get[url]
html_table = BeautifulSoup[r.text].find['table']
r.close[]
df = pd.read_html[str[html_table], header=0][0]
df['Link'] = [link.get['href'] for link in html_table.find_all['a']]
17 or
import requests
import pandas as pd
from bs4 import BeautifulSoup
url = "//legislaturautuado.com/pgs/resolutions.php?st=5&f=2016"
r = requests.get[url]
html_table = BeautifulSoup[r.text].find['table']
r.close[]
df = pd.read_html[str[html_table], header=0][0]
df['Link'] = [link.get['href'] for link in html_table.find_all['a']]
16.Chọn thông qua vị trí của các số nguyên được thông qua:
In [1]: import numpy as np In [2]: import pandas as pd4
Bằng các lát nguyên số, hoạt động tương tự như Numpy/Python:
In [1]: import numpy as np In [2]: import pandas as pd5
Bằng danh sách các vị trí vị trí số nguyên, tương tự như phong cách Numpy/Python:
In [1]: import numpy as np In [2]: import pandas as pd6
Để cắt các hàng một cách rõ ràng:
In [1]: import numpy as np In [2]: import pandas as pd7
Đối với các cột cắt một cách rõ ràng:
In [1]: import numpy as np In [2]: import pandas as pd8
Để nhận được một giá trị rõ ràng:
In [1]: import numpy as np In [2]: import pandas as pd9
Để có quyền truy cập nhanh vào vô hướng [tương đương với phương pháp trước]:
In [3]: s = pd.Series[[1, 3, 5, np.nan, 6, 8]] In [4]: s Out[4]: 0 1.0 1 3.0 2 5.0 3 NaN 4 6.0 5 8.0 dtype: float640
Lựa chọn theo vị trí#
Xem thêm trong lựa chọn theo vị trí bằng cách sử dụng
import requests
import pandas as pd
from bs4 import BeautifulSoup
url = "//legislaturautuado.com/pgs/resolutions.php?st=5&f=2016"
r = requests.get[url]
html_table = BeautifulSoup[r.text].find['table']
r.close[]
df = pd.read_html[str[html_table], header=0][0]
df['Link'] = [link.get['href'] for link in html_table.find_all['a']]
17 hoặc import requests
import pandas as pd
from bs4 import BeautifulSoup
url = "//legislaturautuado.com/pgs/resolutions.php?st=5&f=2016"
r = requests.get[url]
html_table = BeautifulSoup[r.text].find['table']
r.close[]
df = pd.read_html[str[html_table], header=0][0]
df['Link'] = [link.get['href'] for link in html_table.find_all['a']]
16.In [3]: s = pd.Series[[1, 3, 5, np.nan, 6, 8]] In [4]: s Out[4]: 0 1.0 1 3.0 2 5.0 3 NaN 4 6.0 5 8.0 dtype: float641
Chọn thông qua vị trí của các số nguyên được thông qua:
In [3]: s = pd.Series[[1, 3, 5, np.nan, 6, 8]] In [4]: s Out[4]: 0 1.0 1 3.0 2 5.0 3 NaN 4 6.0 5 8.0 dtype: float642
Bằng các lát nguyên số, hoạt động tương tự như Numpy/Python:
In [3]: s = pd.Series[[1, 3, 5, np.nan, 6, 8]] In [4]: s Out[4]: 0 1.0 1 3.0 2 5.0 3 NaN 4 6.0 5 8.0 dtype: float643
Setting#
Đặt cột mới tự động sắp xếp dữ liệu theo các chỉ mục:
In [3]: s = pd.Series[[1, 3, 5, np.nan, 6, 8]] In [4]: s Out[4]: 0 1.0 1 3.0 2 5.0 3 NaN 4 6.0 5 8.0 dtype: float644
Cài đặt giá trị theo nhãn:
In [3]: s = pd.Series[[1, 3, 5, np.nan, 6, 8]] In [4]: s Out[4]: 0 1.0 1 3.0 2 5.0 3 NaN 4 6.0 5 8.0 dtype: float645
Cài đặt giá trị theo vị trí:
In [3]: s = pd.Series[[1, 3, 5, np.nan, 6, 8]] In [4]: s Out[4]: 0 1.0 1 3.0 2 5.0 3 NaN 4 6.0 5 8.0 dtype: float646
Cài đặt bằng cách gán bằng một mảng numpy:
In [3]: s = pd.Series[[1, 3, 5, np.nan, 6, 8]] In [4]: s Out[4]: 0 1.0 1 3.0 2 5.0 3 NaN 4 6.0 5 8.0 dtype: float647
Kết quả của các hoạt động cài đặt trước:
In [3]: s = pd.Series[[1, 3, 5, np.nan, 6, 8]] In [4]: s Out[4]: 0 1.0 1 3.0 2 5.0 3 NaN 4 6.0 5 8.0 dtype: float648
Hoạt động
import requests
import pandas as pd
from bs4 import BeautifulSoup
url = "//legislaturautuado.com/pgs/resolutions.php?st=5&f=2016"
r = requests.get[url]
html_table = BeautifulSoup[r.text].find['table']
r.close[]
df = pd.read_html[str[html_table], header=0][0]
df['Link'] = [link.get['href'] for link in html_table.find_all['a']]
20 với Cài đặt:In [3]: s = pd.Series[[1, 3, 5, np.nan, 6, 8]] In [4]: s Out[4]: 0 1.0 1 3.0 2 5.0 3 NaN 4 6.0 5 8.0 dtype: float649
Dữ liệu bị mất#
Pandas chủ yếu sử dụng giá trị
import requests
import pandas as pd
from bs4 import BeautifulSoup
url = "//legislaturautuado.com/pgs/resolutions.php?st=5&f=2016"
r = requests.get[url]
html_table = BeautifulSoup[r.text].find['table']
r.close[]
df = pd.read_html[str[html_table], header=0][0]
df['Link'] = [link.get['href'] for link in html_table.find_all['a']]
21 để biểu diễn dữ liệu bị thiếu. Đó là mặc định không bao gồm trong tính toán. Xem phần Dữ liệu bị thiếu.Missing Data section.Reindexing cho phép bạn thay đổi/thêm/xóa chỉ mục trên một trục được chỉ định. Điều này trả về một bản sao của dữ liệu:
In [5]: dates = pd.date_range["20130101", periods=6] In [6]: dates Out[6]: DatetimeIndex[['2013-01-01', '2013-01-02', '2013-01-03', '2013-01-04', '2013-01-05', '2013-01-06'], dtype='datetime64[ns]', freq='D'] In [7]: df = pd.DataFrame[np.random.randn[6, 4], index=dates, columns=list["ABCD"]] In [8]: df Out[8]: A B C D 2013-01-01 0.469112 -0.282863 -1.509059 -1.135632 2013-01-02 1.212112 -0.173215 0.119209 -1.044236 2013-01-03 -0.861849 -2.104569 -0.494929 1.071804 2013-01-04 0.721555 -0.706771 -1.039575 0.271860 2013-01-05 -0.424972 0.567020 0.276232 -1.087401 2013-01-06 -0.673690 0.113648 -1.478427 0.5249880
import requests
import pandas as pd
from bs4 import BeautifulSoup
url = "//legislaturautuado.com/pgs/resolutions.php?st=5&f=2016"
r = requests.get[url]
html_table = BeautifulSoup[r.text].find['table']
r.close[]
df = pd.read_html[str[html_table], header=0][0]
df['Link'] = [link.get['href'] for link in html_table.find_all['a']]
22 làm rơi bất kỳ hàng nào bị thiếu dữ liệu:In [5]: dates = pd.date_range["20130101", periods=6] In [6]: dates Out[6]: DatetimeIndex[['2013-01-01', '2013-01-02', '2013-01-03', '2013-01-04', '2013-01-05', '2013-01-06'], dtype='datetime64[ns]', freq='D'] In [7]: df = pd.DataFrame[np.random.randn[6, 4], index=dates, columns=list["ABCD"]] In [8]: df Out[8]: A B C D 2013-01-01 0.469112 -0.282863 -1.509059 -1.135632 2013-01-02 1.212112 -0.173215 0.119209 -1.044236 2013-01-03 -0.861849 -2.104569 -0.494929 1.071804 2013-01-04 0.721555 -0.706771 -1.039575 0.271860 2013-01-05 -0.424972 0.567020 0.276232 -1.087401 2013-01-06 -0.673690 0.113648 -1.478427 0.5249881
import requests
import pandas as pd
from bs4 import BeautifulSoup
url = "//legislaturautuado.com/pgs/resolutions.php?st=5&f=2016"
r = requests.get[url]
html_table = BeautifulSoup[r.text].find['table']
r.close[]
df = pd.read_html[str[html_table], header=0][0]
df['Link'] = [link.get['href'] for link in html_table.find_all['a']]
23 điền vào dữ liệu bị thiếu:In [5]: dates = pd.date_range["20130101", periods=6] In [6]: dates Out[6]: DatetimeIndex[['2013-01-01', '2013-01-02', '2013-01-03', '2013-01-04', '2013-01-05', '2013-01-06'], dtype='datetime64[ns]', freq='D'] In [7]: df = pd.DataFrame[np.random.randn[6, 4], index=dates, columns=list["ABCD"]] In [8]: df Out[8]: A B C D 2013-01-01 0.469112 -0.282863 -1.509059 -1.135632 2013-01-02 1.212112 -0.173215 0.119209 -1.044236 2013-01-03 -0.861849 -2.104569 -0.494929 1.071804 2013-01-04 0.721555 -0.706771 -1.039575 0.271860 2013-01-05 -0.424972 0.567020 0.276232 -1.087401 2013-01-06 -0.673690 0.113648 -1.478427 0.5249882
import requests
import pandas as pd
from bs4 import BeautifulSoup
url = "//legislaturautuado.com/pgs/resolutions.php?st=5&f=2016"
r = requests.get[url]
html_table = BeautifulSoup[r.text].find['table']
r.close[]
df = pd.read_html[str[html_table], header=0][0]
df['Link'] = [link.get['href'] for link in html_table.find_all['a']]
24 Nhận mặt nạ Boolean trong đó các giá trị là import requests
import pandas as pd
from bs4 import BeautifulSoup
url = "//legislaturautuado.com/pgs/resolutions.php?st=5&f=2016"
r = requests.get[url]
html_table = BeautifulSoup[r.text].find['table']
r.close[]
df = pd.read_html[str[html_table], header=0][0]
df['Link'] = [link.get['href'] for link in html_table.find_all['a']]
25:In [5]: dates = pd.date_range["20130101", periods=6] In [6]: dates Out[6]: DatetimeIndex[['2013-01-01', '2013-01-02', '2013-01-03', '2013-01-04', '2013-01-05', '2013-01-06'], dtype='datetime64[ns]', freq='D'] In [7]: df = pd.DataFrame[np.random.randn[6, 4], index=dates, columns=list["ABCD"]] In [8]: df Out[8]: A B C D 2013-01-01 0.469112 -0.282863 -1.509059 -1.135632 2013-01-02 1.212112 -0.173215 0.119209 -1.044236 2013-01-03 -0.861849 -2.104569 -0.494929 1.071804 2013-01-04 0.721555 -0.706771 -1.039575 0.271860 2013-01-05 -0.424972 0.567020 0.276232 -1.087401 2013-01-06 -0.673690 0.113648 -1.478427 0.5249883
Operations#
Xem phần cơ bản trên Ops nhị phân.Basic section on Binary Ops.
Stats#
Hoạt động nói chung loại trừ dữ liệu bị thiếu.
Thực hiện thống kê mô tả:
In [5]: dates = pd.date_range["20130101", periods=6] In [6]: dates Out[6]: DatetimeIndex[['2013-01-01', '2013-01-02', '2013-01-03', '2013-01-04', '2013-01-05', '2013-01-06'], dtype='datetime64[ns]', freq='D'] In [7]: df = pd.DataFrame[np.random.randn[6, 4], index=dates, columns=list["ABCD"]] In [8]: df Out[8]: A B C D 2013-01-01 0.469112 -0.282863 -1.509059 -1.135632 2013-01-02 1.212112 -0.173215 0.119209 -1.044236 2013-01-03 -0.861849 -2.104569 -0.494929 1.071804 2013-01-04 0.721555 -0.706771 -1.039575 0.271860 2013-01-05 -0.424972 0.567020 0.276232 -1.087401 2013-01-06 -0.673690 0.113648 -1.478427 0.5249884
Hoạt động tương tự trên trục khác:
In [5]: dates = pd.date_range["20130101", periods=6] In [6]: dates Out[6]: DatetimeIndex[['2013-01-01', '2013-01-02', '2013-01-03', '2013-01-04', '2013-01-05', '2013-01-06'], dtype='datetime64[ns]', freq='D'] In [7]: df = pd.DataFrame[np.random.randn[6, 4], index=dates, columns=list["ABCD"]] In [8]: df Out[8]: A B C D 2013-01-01 0.469112 -0.282863 -1.509059 -1.135632 2013-01-02 1.212112 -0.173215 0.119209 -1.044236 2013-01-03 -0.861849 -2.104569 -0.494929 1.071804 2013-01-04 0.721555 -0.706771 -1.039575 0.271860 2013-01-05 -0.424972 0.567020 0.276232 -1.087401 2013-01-06 -0.673690 0.113648 -1.478427 0.5249885
Hoạt động với các đối tượng có chiều khác nhau và cần căn chỉnh. Ngoài ra, gấu trúc tự động phát sóng dọc theo kích thước được chỉ định:
In [5]: dates = pd.date_range["20130101", periods=6] In [6]: dates Out[6]: DatetimeIndex[['2013-01-01', '2013-01-02', '2013-01-03', '2013-01-04', '2013-01-05', '2013-01-06'], dtype='datetime64[ns]', freq='D'] In [7]: df = pd.DataFrame[np.random.randn[6, 4], index=dates, columns=list["ABCD"]] In [8]: df Out[8]: A B C D 2013-01-01 0.469112 -0.282863 -1.509059 -1.135632 2013-01-02 1.212112 -0.173215 0.119209 -1.044236 2013-01-03 -0.861849 -2.104569 -0.494929 1.071804 2013-01-04 0.721555 -0.706771 -1.039575 0.271860 2013-01-05 -0.424972 0.567020 0.276232 -1.087401 2013-01-06 -0.673690 0.113648 -1.478427 0.5249886
Apply#
import requests
import pandas as pd
from bs4 import BeautifulSoup
url = "//legislaturautuado.com/pgs/resolutions.php?st=5&f=2016"
r = requests.get[url]
html_table = BeautifulSoup[r.text].find['table']
r.close[]
df = pd.read_html[str[html_table], header=0][0]
df['Link'] = [link.get['href'] for link in html_table.find_all['a']]
26 áp dụng chức năng do người dùng xác định cho dữ liệu:In [5]: dates = pd.date_range["20130101", periods=6] In [6]: dates Out[6]: DatetimeIndex[['2013-01-01', '2013-01-02', '2013-01-03', '2013-01-04', '2013-01-05', '2013-01-06'], dtype='datetime64[ns]', freq='D'] In [7]: df = pd.DataFrame[np.random.randn[6, 4], index=dates, columns=list["ABCD"]] In [8]: df Out[8]: A B C D 2013-01-01 0.469112 -0.282863 -1.509059 -1.135632 2013-01-02 1.212112 -0.173215 0.119209 -1.044236 2013-01-03 -0.861849 -2.104569 -0.494929 1.071804 2013-01-04 0.721555 -0.706771 -1.039575 0.271860 2013-01-05 -0.424972 0.567020 0.276232 -1.087401 2013-01-06 -0.673690 0.113648 -1.478427 0.5249887
Histogramming#
Xem thêm tại Biểu đồ và rời rạc.Histogramming and Discretization.
In [5]: dates = pd.date_range["20130101", periods=6] In [6]: dates Out[6]: DatetimeIndex[['2013-01-01', '2013-01-02', '2013-01-03', '2013-01-04', '2013-01-05', '2013-01-06'], dtype='datetime64[ns]', freq='D'] In [7]: df = pd.DataFrame[np.random.randn[6, 4], index=dates, columns=list["ABCD"]] In [8]: df Out[8]: A B C D 2013-01-01 0.469112 -0.282863 -1.509059 -1.135632 2013-01-02 1.212112 -0.173215 0.119209 -1.044236 2013-01-03 -0.861849 -2.104569 -0.494929 1.071804 2013-01-04 0.721555 -0.706771 -1.039575 0.271860 2013-01-05 -0.424972 0.567020 0.276232 -1.087401 2013-01-06 -0.673690 0.113648 -1.478427 0.5249888
Phương thức chuỗi#
Sê -ri được trang bị một tập hợp các phương thức xử lý chuỗi trong thuộc tính
import requests
import pandas as pd
from bs4 import BeautifulSoup
url = "//legislaturautuado.com/pgs/resolutions.php?st=5&f=2016"
r = requests.get[url]
html_table = BeautifulSoup[r.text].find['table']
r.close[]
df = pd.read_html[str[html_table], header=0][0]
df['Link'] = [link.get['href'] for link in html_table.find_all['a']]
27 giúp dễ dàng vận hành trên mỗi phần tử của mảng, như trong đoạn mã bên dưới. Lưu ý rằng phù hợp với mẫu trong import requests
import pandas as pd
from bs4 import BeautifulSoup
url = "//legislaturautuado.com/pgs/resolutions.php?st=5&f=2016"
r = requests.get[url]
html_table = BeautifulSoup[r.text].find['table']
r.close[]
df = pd.read_html[str[html_table], header=0][0]
df['Link'] = [link.get['href'] for link in html_table.find_all['a']]
27 thường sử dụng các biểu thức thông thường theo mặc định [và trong một số trường hợp luôn sử dụng chúng]. Xem thêm tại các phương thức chuỗi vectorized.Vectorized String Methods.In [5]: dates = pd.date_range["20130101", periods=6] In [6]: dates Out[6]: DatetimeIndex[['2013-01-01', '2013-01-02', '2013-01-03', '2013-01-04', '2013-01-05', '2013-01-06'], dtype='datetime64[ns]', freq='D'] In [7]: df = pd.DataFrame[np.random.randn[6, 4], index=dates, columns=list["ABCD"]] In [8]: df Out[8]: A B C D 2013-01-01 0.469112 -0.282863 -1.509059 -1.135632 2013-01-02 1.212112 -0.173215 0.119209 -1.044236 2013-01-03 -0.861849 -2.104569 -0.494929 1.071804 2013-01-04 0.721555 -0.706771 -1.039575 0.271860 2013-01-05 -0.424972 0.567020 0.276232 -1.087401 2013-01-06 -0.673690 0.113648 -1.478427 0.5249889
Merge#
Concat#
Pandas cung cấp các cơ sở khác nhau để dễ dàng kết hợp các đối tượng sê-ri và DataFrame với nhiều loại logic được đặt khác nhau cho các chỉ mục và chức năng đại số quan hệ trong trường hợp hoạt động loại tham gia / hợp nhất.
Xem phần hợp nhất.Merging section.
Kết nối các đối tượng gấu trúc với nhau dọc theo một trục với
import requests
import pandas as pd
from bs4 import BeautifulSoup
url = "//legislaturautuado.com/pgs/resolutions.php?st=5&f=2016"
r = requests.get[url]
html_table = BeautifulSoup[r.text].find['table']
r.close[]
df = pd.read_html[str[html_table], header=0][0]
df['Link'] = [link.get['href'] for link in html_table.find_all['a']]
29:In [9]: df2 = pd.DataFrame[ ...: { ...: "A": 1.0, ...: "B": pd.Timestamp["20130102"], ...: "C": pd.Series[1, index=list[range[4]], dtype="float32"], ...: "D": np.array[[3] * 4, dtype="int32"], ...: "E": pd.Categorical[["test", "train", "test", "train"]], ...: "F": "foo", ...: } ...: ] ...: In [10]: df2 Out[10]: A B C D E F 0 1.0 2013-01-02 1.0 3 test foo 1 1.0 2013-01-02 1.0 3 train foo 2 1.0 2013-01-02 1.0 3 test foo 3 1.0 2013-01-02 1.0 3 train foo0
Ghi chú
Thêm một cột vào
In [13]: df.head[] Out[13]: A B C D 2013-01-01 0.469112 -0.282863 -1.509059 -1.135632 2013-01-02 1.212112 -0.173215 0.119209 -1.044236 2013-01-03 -0.861849 -2.104569 -0.494929 1.071804 2013-01-04 0.721555 -0.706771 -1.039575 0.271860 2013-01-05 -0.424972 0.567020 0.276232 -1.087401 In [14]: df.tail[3] Out[14]: A B C D 2013-01-04 0.721555 -0.706771 -1.039575 0.271860 2013-01-05 -0.424972 0.567020 0.276232 -1.087401 2013-01-06 -0.673690 0.113648 -1.478427 0.5249884 tương đối nhanh. Tuy nhiên, thêm một hàng yêu cầu một bản sao, và có thể đắt tiền. Chúng tôi khuyên bạn nên chuyển một danh sách các hồ sơ được xây dựng sẵn cho hàm tạo
In [13]: df.head[] Out[13]: A B C D 2013-01-01 0.469112 -0.282863 -1.509059 -1.135632 2013-01-02 1.212112 -0.173215 0.119209 -1.044236 2013-01-03 -0.861849 -2.104569 -0.494929 1.071804 2013-01-04 0.721555 -0.706771 -1.039575 0.271860 2013-01-05 -0.424972 0.567020 0.276232 -1.087401 In [14]: df.tail[3] Out[14]: A B C D 2013-01-04 0.721555 -0.706771 -1.039575 0.271860 2013-01-05 -0.424972 0.567020 0.276232 -1.087401 2013-01-06 -0.673690 0.113648 -1.478427 0.5249884 thay vì xây dựng một
In [13]: df.head[] Out[13]: A B C D 2013-01-01 0.469112 -0.282863 -1.509059 -1.135632 2013-01-02 1.212112 -0.173215 0.119209 -1.044236 2013-01-03 -0.861849 -2.104569 -0.494929 1.071804 2013-01-04 0.721555 -0.706771 -1.039575 0.271860 2013-01-05 -0.424972 0.567020 0.276232 -1.087401 In [14]: df.tail[3] Out[14]: A B C D 2013-01-04 0.721555 -0.706771 -1.039575 0.271860 2013-01-05 -0.424972 0.567020 0.276232 -1.087401 2013-01-06 -0.673690 0.113648 -1.478427 0.5249884 bằng cách lặp lại các hồ sơ nối lại cho nó.
Join#
import requests
import pandas as pd
from bs4 import BeautifulSoup
url = "//legislaturautuado.com/pgs/resolutions.php?st=5&f=2016"
r = requests.get[url]
html_table = BeautifulSoup[r.text].find['table']
r.close[]
df = pd.read_html[str[html_table], header=0][0]
df['Link'] = [link.get['href'] for link in html_table.find_all['a']]
33 cho phép các loại tham gia kiểu SQL theo các cột cụ thể. Xem phần tham gia kiểu cơ sở dữ liệu.Database style joining section.In [9]: df2 = pd.DataFrame[ ...: { ...: "A": 1.0, ...: "B": pd.Timestamp["20130102"], ...: "C": pd.Series[1, index=list[range[4]], dtype="float32"], ...: "D": np.array[[3] * 4, dtype="int32"], ...: "E": pd.Categorical[["test", "train", "test", "train"]], ...: "F": "foo", ...: } ...: ] ...: In [10]: df2 Out[10]: A B C D E F 0 1.0 2013-01-02 1.0 3 test foo 1 1.0 2013-01-02 1.0 3 train foo 2 1.0 2013-01-02 1.0 3 test foo 3 1.0 2013-01-02 1.0 3 train foo1
Một ví dụ khác có thể được đưa ra là:
In [9]: df2 = pd.DataFrame[ ...: { ...: "A": 1.0, ...: "B": pd.Timestamp["20130102"], ...: "C": pd.Series[1, index=list[range[4]], dtype="float32"], ...: "D": np.array[[3] * 4, dtype="int32"], ...: "E": pd.Categorical[["test", "train", "test", "train"]], ...: "F": "foo", ...: } ...: ] ...: In [10]: df2 Out[10]: A B C D E F 0 1.0 2013-01-02 1.0 3 test foo 1 1.0 2013-01-02 1.0 3 train foo 2 1.0 2013-01-02 1.0 3 test foo 3 1.0 2013-01-02 1.0 3 train foo2
Grouping#
Bởi nhóm nhóm của người khác, chúng tôi đang đề cập đến một quy trình liên quan đến một hoặc nhiều bước sau:
Chia dữ liệu thành các nhóm dựa trên một số tiêu chí the data into groups based on some criteria
Áp dụng một chức năng cho mỗi nhóm một cách độc lập a function to each group independently
Kết hợp kết quả thành cấu trúc dữ liệu the results into a data structure
Xem phần nhóm.Grouping section.
In [9]: df2 = pd.DataFrame[ ...: { ...: "A": 1.0, ...: "B": pd.Timestamp["20130102"], ...: "C": pd.Series[1, index=list[range[4]], dtype="float32"], ...: "D": np.array[[3] * 4, dtype="int32"], ...: "E": pd.Categorical[["test", "train", "test", "train"]], ...: "F": "foo", ...: } ...: ] ...: In [10]: df2 Out[10]: A B C D E F 0 1.0 2013-01-02 1.0 3 test foo 1 1.0 2013-01-02 1.0 3 train foo 2 1.0 2013-01-02 1.0 3 test foo 3 1.0 2013-01-02 1.0 3 train foo3
Nhóm và sau đó áp dụng chức năng
import requests
import pandas as pd
from bs4 import BeautifulSoup
url = "//legislaturautuado.com/pgs/resolutions.php?st=5&f=2016"
r = requests.get[url]
html_table = BeautifulSoup[r.text].find['table']
r.close[]
df = pd.read_html[str[html_table], header=0][0]
df['Link'] = [link.get['href'] for link in html_table.find_all['a']]
34 cho các nhóm kết quả:In [9]: df2 = pd.DataFrame[ ...: { ...: "A": 1.0, ...: "B": pd.Timestamp["20130102"], ...: "C": pd.Series[1, index=list[range[4]], dtype="float32"], ...: "D": np.array[[3] * 4, dtype="int32"], ...: "E": pd.Categorical[["test", "train", "test", "train"]], ...: "F": "foo", ...: } ...: ] ...: In [10]: df2 Out[10]: A B C D E F 0 1.0 2013-01-02 1.0 3 test foo 1 1.0 2013-01-02 1.0 3 train foo 2 1.0 2013-01-02 1.0 3 test foo 3 1.0 2013-01-02 1.0 3 train foo4
Nhóm theo nhiều cột tạo thành một chỉ mục phân cấp và một lần nữa chúng ta có thể áp dụng chức năng
import requests
import pandas as pd
from bs4 import BeautifulSoup
url = "//legislaturautuado.com/pgs/resolutions.php?st=5&f=2016"
r = requests.get[url]
html_table = BeautifulSoup[r.text].find['table']
r.close[]
df = pd.read_html[str[html_table], header=0][0]
df['Link'] = [link.get['href'] for link in html_table.find_all['a']]
34:In [9]: df2 = pd.DataFrame[ ...: { ...: "A": 1.0, ...: "B": pd.Timestamp["20130102"], ...: "C": pd.Series[1, index=list[range[4]], dtype="float32"], ...: "D": np.array[[3] * 4, dtype="int32"], ...: "E": pd.Categorical[["test", "train", "test", "train"]], ...: "F": "foo", ...: } ...: ] ...: In [10]: df2 Out[10]: A B C D E F 0 1.0 2013-01-02 1.0 3 test foo 1 1.0 2013-01-02 1.0 3 train foo 2 1.0 2013-01-02 1.0 3 test foo 3 1.0 2013-01-02 1.0 3 train foo5
Reshaping#
Xem các phần về lập chỉ mục phân cấp và định hình lại.Hierarchical Indexing and Reshaping.
Stack#
In [9]: df2 = pd.DataFrame[ ...: { ...: "A": 1.0, ...: "B": pd.Timestamp["20130102"], ...: "C": pd.Series[1, index=list[range[4]], dtype="float32"], ...: "D": np.array[[3] * 4, dtype="int32"], ...: "E": pd.Categorical[["test", "train", "test", "train"]], ...: "F": "foo", ...: } ...: ] ...: In [10]: df2 Out[10]: A B C D E F 0 1.0 2013-01-02 1.0 3 test foo 1 1.0 2013-01-02 1.0 3 train foo 2 1.0 2013-01-02 1.0 3 test foo 3 1.0 2013-01-02 1.0 3 train foo6
Phương thức
import requests
import pandas as pd
from bs4 import BeautifulSoup
url = "//legislaturautuado.com/pgs/resolutions.php?st=5&f=2016"
r = requests.get[url]
html_table = BeautifulSoup[r.text].find['table']
r.close[]
df = pd.read_html[str[html_table], header=0][0]
df['Link'] = [link.get['href'] for link in html_table.find_all['a']]
36, nén một cấp độ trong các cột DataFrame:In [9]: df2 = pd.DataFrame[ ...: { ...: "A": 1.0, ...: "B": pd.Timestamp["20130102"], ...: "C": pd.Series[1, index=list[range[4]], dtype="float32"], ...: "D": np.array[[3] * 4, dtype="int32"], ...: "E": pd.Categorical[["test", "train", "test", "train"]], ...: "F": "foo", ...: } ...: ] ...: In [10]: df2 Out[10]: A B C D E F 0 1.0 2013-01-02 1.0 3 test foo 1 1.0 2013-01-02 1.0 3 train foo 2 1.0 2013-01-02 1.0 3 test foo 3 1.0 2013-01-02 1.0 3 train foo7
Với một loạt hoặc loạt dữ liệu được xếp chồng lên nhau [có
import requests
import pandas as pd
from bs4 import BeautifulSoup
url = "//legislaturautuado.com/pgs/resolutions.php?st=5&f=2016"
r = requests.get[url]
html_table = BeautifulSoup[r.text].find['table']
r.close[]
df = pd.read_html[str[html_table], header=0][0]
df['Link'] = [link.get['href'] for link in html_table.find_all['a']]
37 là import requests
import pandas as pd
from bs4 import BeautifulSoup
url = "//legislaturautuado.com/pgs/resolutions.php?st=5&f=2016"
r = requests.get[url]
html_table = BeautifulSoup[r.text].find['table']
r.close[]
df = pd.read_html[str[html_table], header=0][0]
df['Link'] = [link.get['href'] for link in html_table.find_all['a']]
38], hoạt động nghịch đảo của import requests
import pandas as pd
from bs4 import BeautifulSoup
url = "//legislaturautuado.com/pgs/resolutions.php?st=5&f=2016"
r = requests.get[url]
html_table = BeautifulSoup[r.text].find['table']
r.close[]
df = pd.read_html[str[html_table], header=0][0]
df['Link'] = [link.get['href'] for link in html_table.find_all['a']]
36 là import requests
import pandas as pd
from bs4 import BeautifulSoup
url = "//legislaturautuado.com/pgs/resolutions.php?st=5&f=2016"
r = requests.get[url]
html_table = BeautifulSoup[r.text].find['table']
r.close[]
df = pd.read_html[str[html_table], header=0][0]
df['Link'] = [link.get['href'] for link in html_table.find_all['a']]
40, theo mặc định không thể giải quyết được cấp độ cuối cùng:last level:In [9]: df2 = pd.DataFrame[ ...: { ...: "A": 1.0, ...: "B": pd.Timestamp["20130102"], ...: "C": pd.Series[1, index=list[range[4]], dtype="float32"], ...: "D": np.array[[3] * 4, dtype="int32"], ...: "E": pd.Categorical[["test", "train", "test", "train"]], ...: "F": "foo", ...: } ...: ] ...: In [10]: df2 Out[10]: A B C D E F 0 1.0 2013-01-02 1.0 3 test foo 1 1.0 2013-01-02 1.0 3 train foo 2 1.0 2013-01-02 1.0 3 test foo 3 1.0 2013-01-02 1.0 3 train foo8
Bảng xoay vòng#
Xem phần trên bảng trục.Pivot Tables.
In [9]: df2 = pd.DataFrame[ ...: { ...: "A": 1.0, ...: "B": pd.Timestamp["20130102"], ...: "C": pd.Series[1, index=list[range[4]], dtype="float32"], ...: "D": np.array[[3] * 4, dtype="int32"], ...: "E": pd.Categorical[["test", "train", "test", "train"]], ...: "F": "foo", ...: } ...: ] ...: In [10]: df2 Out[10]: A B C D E F 0 1.0 2013-01-02 1.0 3 test foo 1 1.0 2013-01-02 1.0 3 train foo 2 1.0 2013-01-02 1.0 3 test foo 3 1.0 2013-01-02 1.0 3 train foo9
import requests
import pandas as pd
from bs4 import BeautifulSoup
url = "//legislaturautuado.com/pgs/resolutions.php?st=5&f=2016"
r = requests.get[url]
html_table = BeautifulSoup[r.text].find['table']
r.close[]
df = pd.read_html[str[html_table], header=0][0]
df['Link'] = [link.get['href'] for link in html_table.find_all['a']]
41 Xoay A In [13]: df.head[] Out[13]: A B C D 2013-01-01 0.469112 -0.282863 -1.509059 -1.135632 2013-01-02 1.212112 -0.173215 0.119209 -1.044236 2013-01-03 -0.861849 -2.104569 -0.494929 1.071804 2013-01-04 0.721555 -0.706771 -1.039575 0.271860 2013-01-05 -0.424972 0.567020 0.276232 -1.087401 In [14]: df.tail[3] Out[14]: A B C D 2013-01-04 0.721555 -0.706771 -1.039575 0.271860 2013-01-05 -0.424972 0.567020 0.276232 -1.087401 2013-01-06 -0.673690 0.113648 -1.478427 0.5249884 Chỉ định
import requests
import pandas as pd
from bs4 import BeautifulSoup
url = "//legislaturautuado.com/pgs/resolutions.php?st=5&f=2016"
r = requests.get[url]
html_table = BeautifulSoup[r.text].find['table']
r.close[]
df = pd.read_html[str[html_table], header=0][0]
df['Link'] = [link.get['href'] for link in html_table.find_all['a']]
43, import requests
import pandas as pd
from bs4 import BeautifulSoup
url = "//legislaturautuado.com/pgs/resolutions.php?st=5&f=2016"
r = requests.get[url]
html_table = BeautifulSoup[r.text].find['table']
r.close[]
df = pd.read_html[str[html_table], header=0][0]
df['Link'] = [link.get['href'] for link in html_table.find_all['a']]
38 và import requests
import pandas as pd
from bs4 import BeautifulSoup
url = "//legislaturautuado.com/pgs/resolutions.php?st=5&f=2016"
r = requests.get[url]
html_table = BeautifulSoup[r.text].find['table']
r.close[]
df = pd.read_html[str[html_table], header=0][0]
df['Link'] = [link.get['href'] for link in html_table.find_all['a']]
45In [11]: df2.dtypes Out[11]: A float64 B datetime64[ns] C float32 D int32 E category F object dtype: object0
Chuỗi thời gian#
Pandas có chức năng đơn giản, mạnh mẽ và hiệu quả để thực hiện các hoạt động lấy mẫu lại trong quá trình chuyển đổi tần số [ví dụ: chuyển đổi dữ liệu thứ hai thành dữ liệu 5 phút]. Điều này là cực kỳ phổ biến trong, nhưng không giới hạn ở các ứng dụng tài chính. Xem phần chuỗi thời gian.Time Series section.
In [11]: df2.dtypes Out[11]: A float64 B datetime64[ns] C float32 D int32 E category F object dtype: object1
import requests
import pandas as pd
from bs4 import BeautifulSoup
url = "//legislaturautuado.com/pgs/resolutions.php?st=5&f=2016"
r = requests.get[url]
html_table = BeautifulSoup[r.text].find['table']
r.close[]
df = pd.read_html[str[html_table], header=0][0]
df['Link'] = [link.get['href'] for link in html_table.find_all['a']]
46 định vị chuỗi thời gian vào múi giờ:In [11]: df2.dtypes Out[11]: A float64 B datetime64[ns] C float32 D int32 E category F object dtype: object2
import requests
import pandas as pd
from bs4 import BeautifulSoup
url = "//legislaturautuado.com/pgs/resolutions.php?st=5&f=2016"
r = requests.get[url]
html_table = BeautifulSoup[r.text].find['table']
r.close[]
df = pd.read_html[str[html_table], header=0][0]
df['Link'] = [link.get['href'] for link in html_table.find_all['a']]
47 Chuyển đổi chuỗi thời gian Aware Time Aware thành múi giờ khác:In [11]: df2.dtypes Out[11]: A float64 B datetime64[ns] C float32 D int32 E category F object dtype: object3
Chuyển đổi giữa các biểu diễn khoảng thời gian:
In [11]: df2.dtypes Out[11]: A float64 B datetime64[ns] C float32 D int32 E category F object dtype: object4
Chuyển đổi giữa giai đoạn và dấu thời gian cho phép sử dụng một số chức năng số học thuận tiện. Trong ví dụ sau, chúng tôi chuyển đổi tần số hàng quý với năm kết thúc vào tháng 11 đến 9 giờ tối cuối tháng sau khi kết thúc quý:
In [11]: df2.dtypes Out[11]: A float64 B datetime64[ns] C float32 D int32 E category F object dtype: object5
Categoricals#
Pandas có thể bao gồm dữ liệu phân loại trong
In [13]: df.head[] Out[13]: A B C D 2013-01-01 0.469112 -0.282863 -1.509059 -1.135632 2013-01-02 1.212112 -0.173215 0.119209 -1.044236 2013-01-03 -0.861849 -2.104569 -0.494929 1.071804 2013-01-04 0.721555 -0.706771 -1.039575 0.271860 2013-01-05 -0.424972 0.567020 0.276232 -1.087401 In [14]: df.tail[3] Out[14]: A B C D 2013-01-04 0.721555 -0.706771 -1.039575 0.271860 2013-01-05 -0.424972 0.567020 0.276232 -1.087401 2013-01-06 -0.673690 0.113648 -1.478427 0.5249884. Để biết đầy đủ các tài liệu, xem phần giới thiệu phân loại và tài liệu API.categorical introduction and the API documentation.
In [11]: df2.dtypes Out[11]: A float64 B datetime64[ns] C float32 D int32 E category F object dtype: object6
Chuyển đổi các lớp thô thành loại dữ liệu phân loại:
In [11]: df2.dtypes Out[11]: A float64 B datetime64[ns] C float32 D int32 E category F object dtype: object7
Đổi tên các danh mục thành các tên có ý nghĩa hơn:
In [11]: df2.dtypes Out[11]: A float64 B datetime64[ns] C float32 D int32 E category F object dtype: object8
Sắp xếp lại các danh mục và đồng thời thêm các danh mục bị thiếu [các phương thức trong
import requests
import pandas as pd
from bs4 import BeautifulSoup
url = "//legislaturautuado.com/pgs/resolutions.php?st=5&f=2016"
r = requests.get[url]
html_table = BeautifulSoup[r.text].find['table']
r.close[]
df = pd.read_html[str[html_table], header=0][0]
df['Link'] = [link.get['href'] for link in html_table.find_all['a']]
49 Trả về một In [13]: df.head[] Out[13]: A B C D 2013-01-01 0.469112 -0.282863 -1.509059 -1.135632 2013-01-02 1.212112 -0.173215 0.119209 -1.044236 2013-01-03 -0.861849 -2.104569 -0.494929 1.071804 2013-01-04 0.721555 -0.706771 -1.039575 0.271860 2013-01-05 -0.424972 0.567020 0.276232 -1.087401 In [14]: df.tail[3] Out[14]: A B C D 2013-01-04 0.721555 -0.706771 -1.039575 0.271860 2013-01-05 -0.424972 0.567020 0.276232 -1.087401 2013-01-06 -0.673690 0.113648 -1.478427 0.5249883 mới theo mặc định]:
In [11]: df2.dtypes Out[11]: A float64 B datetime64[ns] C float32 D int32 E category F object dtype: object9
Sắp xếp theo đơn đặt hàng trong các danh mục, không phải thứ tự từ vựng:
In [12]: df2. # noqa: E225, E999 df2.A df2.bool df2.abs df2.boxplot df2.add df2.C df2.add_prefix df2.clip df2.add_suffix df2.columns df2.align df2.copy df2.all df2.count df2.any df2.combine df2.append df2.D df2.apply df2.describe df2.applymap df2.diff df2.B df2.duplicated0
Nhóm theo một cột phân loại cũng hiển thị các danh mục trống:
In [12]: df2. # noqa: E225, E999 df2.A df2.bool df2.abs df2.boxplot df2.add df2.C df2.add_prefix df2.clip df2.add_suffix df2.columns df2.align df2.copy df2.all df2.count df2.any df2.combine df2.append df2.D df2.apply df2.describe df2.applymap df2.diff df2.B df2.duplicated1
Plotting#
Xem các tài liệu âm mưu.Plotting docs.
Chúng tôi sử dụng quy ước tiêu chuẩn để tham khảo API Matplotlib:
In [12]: df2. # noqa: E225, E999 df2.A df2.bool df2.abs df2.boxplot df2.add df2.C df2.add_prefix df2.clip df2.add_suffix df2.columns df2.align df2.copy df2.all df2.count df2.any df2.combine df2.append df2.D df2.apply df2.describe df2.applymap df2.diff df2.B df2.duplicated2
Phương thức
import requests
import pandas as pd
from bs4 import BeautifulSoup
url = "//legislaturautuado.com/pgs/resolutions.php?st=5&f=2016"
r = requests.get[url]
html_table = BeautifulSoup[r.text].find['table']
r.close[]
df = pd.read_html[str[html_table], header=0][0]
df['Link'] = [link.get['href'] for link in html_table.find_all['a']]
51 được sử dụng để đóng cửa sổ hình:In [12]: df2. # noqa: E225, E999 df2.A df2.bool df2.abs df2.boxplot df2.add df2.C df2.add_prefix df2.clip df2.add_suffix df2.columns df2.align df2.copy df2.all df2.count df2.any df2.combine df2.append df2.D df2.apply df2.describe df2.applymap df2.diff df2.B df2.duplicated3
Nếu chạy dưới Notebook Jupyter, cốt truyện sẽ xuất hiện trên
import requests
import pandas as pd
from bs4 import BeautifulSoup
url = "//legislaturautuado.com/pgs/resolutions.php?st=5&f=2016"
r = requests.get[url]
html_table = BeautifulSoup[r.text].find['table']
r.close[]
df = pd.read_html[str[html_table], header=0][0]
df['Link'] = [link.get['href'] for link in html_table.find_all['a']]
52. Mặt khác, sử dụng matplotlib.pyplot.show để hiển thị nó hoặc matplotlib.pyplot.savefig để ghi nó vào một tệp.Trên DataFrame, phương thức
import requests
import pandas as pd
from bs4 import BeautifulSoup
url = "//legislaturautuado.com/pgs/resolutions.php?st=5&f=2016"
r = requests.get[url]
html_table = BeautifulSoup[r.text].find['table']
r.close[]
df = pd.read_html[str[html_table], header=0][0]
df['Link'] = [link.get['href'] for link in html_table.find_all['a']]
52 là một sự tiện lợi để vẽ tất cả các cột với nhãn:In [12]: df2. # noqa: E225, E999 df2.A df2.bool df2.abs df2.boxplot df2.add df2.C df2.add_prefix df2.clip df2.add_suffix df2.columns df2.align df2.copy df2.all df2.count df2.any df2.combine df2.append df2.D df2.apply df2.describe df2.applymap df2.diff df2.B df2.duplicated4
Nhập và xuất dữ liệu#
CSV#
Viết vào tệp CSV: Sử dụng
54 using import requests
import pandas as pd
from bs4 import BeautifulSoup
url = "//legislaturautuado.com/pgs/resolutions.php?st=5&f=2016"
r = requests.get[url]
html_table = BeautifulSoup[r.text].find['table']
r.close[]
df = pd.read_html[str[html_table], header=0][0]
df['Link'] = [link.get['href'] for link in html_table.find_all['a']]
import requests
import pandas as pd
from bs4 import BeautifulSoup
url = "//legislaturautuado.com/pgs/resolutions.php?st=5&f=2016"
r = requests.get[url]
html_table = BeautifulSoup[r.text].find['table']
r.close[]
df = pd.read_html[str[html_table], header=0][0]
df['Link'] = [link.get['href'] for link in html_table.find_all['a']]
54In [12]: df2. # noqa: E225, E999 df2.A df2.bool df2.abs df2.boxplot df2.add df2.C df2.add_prefix df2.clip df2.add_suffix df2.columns df2.align df2.copy df2.all df2.count df2.any df2.combine df2.append df2.D df2.apply df2.describe df2.applymap df2.diff df2.B df2.duplicated5
Đọc từ tệp CSV: Sử dụng
55 using import requests
import pandas as pd
from bs4 import BeautifulSoup
url = "//legislaturautuado.com/pgs/resolutions.php?st=5&f=2016"
r = requests.get[url]
html_table = BeautifulSoup[r.text].find['table']
r.close[]
df = pd.read_html[str[html_table], header=0][0]
df['Link'] = [link.get['href'] for link in html_table.find_all['a']]
import requests
import pandas as pd
from bs4 import BeautifulSoup
url = "//legislaturautuado.com/pgs/resolutions.php?st=5&f=2016"
r = requests.get[url]
html_table = BeautifulSoup[r.text].find['table']
r.close[]
df = pd.read_html[str[html_table], header=0][0]
df['Link'] = [link.get['href'] for link in html_table.find_all['a']]
55In [12]: df2. # noqa: E225, E999 df2.A df2.bool df2.abs df2.boxplot df2.add df2.C df2.add_prefix df2.clip df2.add_suffix df2.columns df2.align df2.copy df2.all df2.count df2.any df2.combine df2.append df2.D df2.apply df2.describe df2.applymap df2.diff df2.B df2.duplicated6
HDF5#
Đọc và viết cho HDFSTORES.HDFStores.
Viết cho một cửa hàng HDF5 bằng
import requests
import pandas as pd
from bs4 import BeautifulSoup
url = "//legislaturautuado.com/pgs/resolutions.php?st=5&f=2016"
r = requests.get[url]
html_table = BeautifulSoup[r.text].find['table']
r.close[]
df = pd.read_html[str[html_table], header=0][0]
df['Link'] = [link.get['href'] for link in html_table.find_all['a']]
56:In [12]: df2. # noqa: E225, E999 df2.A df2.bool df2.abs df2.boxplot df2.add df2.C df2.add_prefix df2.clip df2.add_suffix df2.columns df2.align df2.copy df2.all df2.count df2.any df2.combine df2.append df2.D df2.apply df2.describe df2.applymap df2.diff df2.B df2.duplicated7
Đọc từ cửa hàng HDF5 bằng
import requests
import pandas as pd
from bs4 import BeautifulSoup
url = "//legislaturautuado.com/pgs/resolutions.php?st=5&f=2016"
r = requests.get[url]
html_table = BeautifulSoup[r.text].find['table']
r.close[]
df = pd.read_html[str[html_table], header=0][0]
df['Link'] = [link.get['href'] for link in html_table.find_all['a']]
57:In [12]: df2. # noqa: E225, E999 df2.A df2.bool df2.abs df2.boxplot df2.add df2.C df2.add_prefix df2.clip df2.add_suffix df2.columns df2.align df2.copy df2.all df2.count df2.any df2.combine df2.append df2.D df2.apply df2.describe df2.applymap df2.diff df2.B df2.duplicated8
Excel#
Đọc và viết để Excel.Excel.
Ghi vào tệp Excel bằng
import requests
import pandas as pd
from bs4 import BeautifulSoup
url = "//legislaturautuado.com/pgs/resolutions.php?st=5&f=2016"
r = requests.get[url]
html_table = BeautifulSoup[r.text].find['table']
r.close[]
df = pd.read_html[str[html_table], header=0][0]
df['Link'] = [link.get['href'] for link in html_table.find_all['a']]
58:In [12]: df2. # noqa: E225, E999 df2.A df2.bool df2.abs df2.boxplot df2.add df2.C df2.add_prefix df2.clip df2.add_suffix df2.columns df2.align df2.copy df2.all df2.count df2.any df2.combine df2.append df2.D df2.apply df2.describe df2.applymap df2.diff df2.B df2.duplicated9
Đọc từ tệp Excel bằng
import requests
import pandas as pd
from bs4 import BeautifulSoup
url = "//legislaturautuado.com/pgs/resolutions.php?st=5&f=2016"
r = requests.get[url]
html_table = BeautifulSoup[r.text].find['table']
r.close[]
df = pd.read_html[str[html_table], header=0][0]
df['Link'] = [link.get['href'] for link in html_table.find_all['a']]
59:In [13]: df.head[] Out[13]: A B C D 2013-01-01 0.469112 -0.282863 -1.509059 -1.135632 2013-01-02 1.212112 -0.173215 0.119209 -1.044236 2013-01-03 -0.861849 -2.104569 -0.494929 1.071804 2013-01-04 0.721555 -0.706771 -1.039575 0.271860 2013-01-05 -0.424972 0.567020 0.276232 -1.087401 In [14]: df.tail[3] Out[14]: A B C D 2013-01-04 0.721555 -0.706771 -1.039575 0.271860 2013-01-05 -0.424972 0.567020 0.276232 -1.087401 2013-01-06 -0.673690 0.113648 -1.478427 0.5249880
Gotchas#
Nếu bạn đang cố gắng thực hiện thao tác Boolean trên
In [13]: df.head[] Out[13]: A B C D 2013-01-01 0.469112 -0.282863 -1.509059 -1.135632 2013-01-02 1.212112 -0.173215 0.119209 -1.044236 2013-01-03 -0.861849 -2.104569 -0.494929 1.071804 2013-01-04 0.721555 -0.706771 -1.039575 0.271860 2013-01-05 -0.424972 0.567020 0.276232 -1.087401 In [14]: df.tail[3] Out[14]: A B C D 2013-01-04 0.721555 -0.706771 -1.039575 0.271860 2013-01-05 -0.424972 0.567020 0.276232 -1.087401 2013-01-06 -0.673690 0.113648 -1.478427 0.5249883 hoặc
In [13]: df.head[] Out[13]: A B C D 2013-01-01 0.469112 -0.282863 -1.509059 -1.135632 2013-01-02 1.212112 -0.173215 0.119209 -1.044236 2013-01-03 -0.861849 -2.104569 -0.494929 1.071804 2013-01-04 0.721555 -0.706771 -1.039575 0.271860 2013-01-05 -0.424972 0.567020 0.276232 -1.087401 In [14]: df.tail[3] Out[14]: A B C D 2013-01-04 0.721555 -0.706771 -1.039575 0.271860 2013-01-05 -0.424972 0.567020 0.276232 -1.087401 2013-01-06 -0.673690 0.113648 -1.478427 0.5249884, bạn có thể thấy một ngoại lệ như:
In [13]: df.head[] Out[13]: A B C D 2013-01-01 0.469112 -0.282863 -1.509059 -1.135632 2013-01-02 1.212112 -0.173215 0.119209 -1.044236 2013-01-03 -0.861849 -2.104569 -0.494929 1.071804 2013-01-04 0.721555 -0.706771 -1.039575 0.271860 2013-01-05 -0.424972 0.567020 0.276232 -1.087401 In [14]: df.tail[3] Out[14]: A B C D 2013-01-04 0.721555 -0.706771 -1.039575 0.271860 2013-01-05 -0.424972 0.567020 0.276232 -1.087401 2013-01-06 -0.673690 0.113648 -1.478427 0.5249881
Xem so sánh và gotchas để giải thích và phải làm gì.Comparisons and Gotchas for an explanation and what to do.