Coelll
Coelll

Reputation: 55

How to scrape a table with multiple headers?

Table

I am trying to scrape a table and display it in pycharm, but scraping it gives me "keyerror".

import pandas as pd
pd.set_option('display.max_columns', None)
pd.set_option('display.width', None)
pd.set_option('display.max_colwidth', None)

url = pd.read_html("https://azurlane.koumakan.jp/List_of_Large_Cruiser_Guns")

#   User input and lists with table headers
eq_name = input(str())
df = url[1]
table_headers = {'Unnamed: 2': 'Firepower', 'Unnamed: 3': 'Anti-air'}
display_data = ['Equipment', 'Firepower', 'Anti-air', 'Rnd', 'Dmg', 'Coef', 'VT', 'Rld',
            'Surface DPS', 'Rng', 'Sprd', 'Angle', 'Attr', 'Ammo']
df.rename(columns=table_headers, inplace=True)
df.fillna('0', inplace=True)
df.reset_index(drop=True, inplace=True)

#   Large cruisers triple 283mm (sk c/28)
if eq_name == "Triple 283mm (SK C/28)".casefold():
    df = df.loc[[0]]
    name_notes = df[display_data]
    print(name_notes)

Upvotes: 0

Views: 110

Answers (1)

Ynjxsjmh
Ynjxsjmh

Reputation: 30022

Your columns mapper doesn't match the column names, try with

table_headers = {'Unnamed: 2_level_0' : 'Firepower',
                 'Unnamed: 3_level_0' : 'Anti-air',
                 'Unnamed: 2_level_1': 'Firepower',
                 'Unnamed: 3_level_1': 'Anti-air'
                 }

Upvotes: 1

Related Questions