Search code examples
pythonpandaslistweb-scraping

Split scrapped table into lists with pandas


So I have a basic 3 lines here. Gives me the info I want when I print airlines[3] but if I put it in a for loop like 'for i in airlines[3]:' it only prints out Airlines and Destinations. I want to take all the airlines put them into their own list and I want all the destinations in their own list too.

import pandas as pd
airlines = pd.read_html('https://en.wikipedia.org/wiki/Tan_Son_Nhat_International_Airport')

# Prints off table
print(airlines[3])


airlines_list = []
destination_list = []

Solution

  • airlines[3] is a pandas dataframe. Use below commands to create separate lists:

    In [1606]: df = airlines[3]
    
    In [1607]: airlines_list = df['Airlines'].tolist()
    
    In [1609]: destination_list = df['Destinations'].tolist()