我的代码进入维基百科页面,并打印出我想要的表格。但是,假设我想要该表的第 n、n-1 和 n-2 列。
我该怎么做?
from selenium import webdriver
import pandas as pd
driver = webdriver.Chrome()
val=[]
webPage=driver.get('https://en.wikipedia.org/wiki/Economy_of_the_United_States#Data')
df=pd.read_html('https://en.wikipedia.org/wiki/Economy_of_the_United_States#Data')[1]
print(df)
答案 0 :(得分:0)
df.iloc[:, n]
对于最后一列,您可以使用
df.iloc[:, -1]
示例
>>> df
0 1 2 3
0 1 2 9 10
1 3 4 8 11
2 5 6 7 12
>>> df.iloc[:, -3:]
1 2 3
0 2 9 10
1 4 8 11
2 6 7 12