'Python: dataframe from read_html with multiple tables, how to join multiple tables accessing by the index?

I get data from https://www.numbeo.com/cost-of-living/city-history/in/Rio-De-Janeiro with read_html, but I don't know how to join the 15 tables there using for loop.


dfs = pd.read_html(url, header=0, index_col=0)
print(len(dfs)) # output: 15

# what I did, but that will duplicate some dataframes
for i in range(14):
    result = pd.concat([dfs[i], dfs[i+1]], axis=1)
    print(result)

How to iterate with the 15 tables and join all to one data frame. All have the same first column: "Year"



Solution 1:[1]

This will work

dfs = pd.read_html(url, header=0, index_col=0)
merge_df = pd.DataFrame(dfs[1])
for i in range(2, len(dfs)):
    merge_df = merge_df.join(dfs[i], rsuffix = '_y')
    
merge_df

The first table is formatted weirdly so I skipped it, and I started the df with a prep dataframe of the first table on the page

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 ArchAngelPwn