简体   繁体   中英

Im trying to extract data from a URL in Python and geeting this error when trying to find the most common data source

This is my code

import pandas as pd

# Read the data from the Wikipedia page into a Pandas DataFrame
url = "https://en.wikipedia.org/wiki/List_of_countries_and_dependencies_by_population"
df = pd.read_html(url, attrs={"class": "wikitable"})[0]

# Visualize the DataFrame
print(df)

# Print the number of records in the DataFrame
print(f"There are {len(df)} records in the DataFrame.")


# Find the most common data source
most_common_source = df["Source"].value_counts().index[0]

print(f"The most common data source is {most_common_source}.")

KeyError Traceback (most recent call last) /usr/local/lib/python3.8/dist-packages/pandas/core/indexes/base.py in get_loc(self, key, method, tolerance) 3360 try: -> 3361 return self._engine.get_loc(casted_key) 3362 except KeyError as err:

8 frames pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()

pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()

KeyError: 'Source'

The above exception was the direct cause of the following exception:

KeyError Traceback (most recent call last) /usr/local/lib/python3.8/dist-packages/pandas/core/indexes/base.py in get_loc(self, key, method, tolerance) 3361 return self._engine.get_loc(casted_key) 3362 except KeyError as err: -> 3363 raise KeyError(key) from err 3364 3365 if is_scalar(key) and isna(key) and not self.hasnans:

KeyError: 'Source'

The table you are trying to extract from does not have a column named Source . Maybe you meant something like this:

most_common_source = \
    df["Source (official or from the\xa0United Nations)"].value_counts().index[0]

You can always print your columns with list(df.columns) .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM