2

My code gets into a wikipedia page, and prints the table I want. However, say i want the nth, n-1, and n-2 columns of this table.

How can i do this?

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)
Void S
  • 752
  • 4
  • 14

1 Answers1

1

Use pandas.DataFrame.iloc

df.iloc[:, n]

For the last column, you can use

df.iloc[:, -1]

Example

>>> 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
Vishnudev Krishnadas
  • 10,679
  • 2
  • 23
  • 55