Ashish Agarwal
Ashish Agarwal

Reputation: 6283

How to get Dataframe with Table ID in Pandas?

I want to extract dataframe from HTML using URL.

Following is my trail which is giving error.

import pandas as pd
df = pd.read_html("http://eciresults.nic.in/statewiseS12.htm?st=S12",attrs = {'id': 'ctl00_Menu1'})

enter image description here

As this is my very early stage in python so can be simple solution but I am unable to find. appreciate help.

Upvotes: 0

Views: 2939

Answers (1)

Matías Romo
Matías Romo

Reputation: 100

I would look at how the URL passes params and probably try to read a dataframe directly from it. I'm unsure if you are trying to develop a function or a script or just exercising.

If you do (notice the 58 at the end of the url)

df = pd.read_html("http://eciresults.nic.in/statewiseS12.htm?st=S1258",attrs = {'id': 
'ctl00_Menu1'})

It works and gives you table 59.

[                                  0          1   2  \
 0                         Partywise  Partywise NaN   
 1                         Partywise        NaN NaN   
 2  Constituencywise-All  Candidates        NaN NaN   
 3           Constituencywise Trends        NaN NaN   

                                   3                                 4   5  \
 0  Constituencywise-All  Candidates  Constituencywise-All  Candidates NaN   
 1                               NaN                               NaN NaN   
 2                               NaN                               NaN NaN   
 3                               NaN                               NaN NaN   

                          6                        7  
 0  Constituencywise Trends  Constituencywise Trends  
 1                      NaN                      NaN  
 2                      NaN                      NaN  
 3                      NaN                      NaN  ]

Unsure if that's the table you want to extract, but most of the time it's easier to pass it as a url parameter. If you try it without the 58 it works too, I believe the 'ElectionResult' argument might not be a table classifier hence why you can't find any tables with that name.

Upvotes: 3

Related Questions