简体   繁体   中英

Need help converting dataframe column into list

I am trying to convert the 'Symbol' column from the selected table in to a list as below. However, it does not seem to work. Could anyone please explain why this is not working?

df = pd.read_html('https://en.wikipedia.org/wiki/S%26P/ASX_20')
table = df[0]
symbols = table['Symbol']

tickers = symbols.to_list()

print(tickers)
print(type(tickers))

This is the error I get:

AttributeError: 'DataFrame' object has no attribute 'to_list'

This is due to the fact that symbols is DataFrame:

import pandas as pd

df = pd.read_html('https://en.wikipedia.org/wiki/S%26P/ASX_20')
table = df[0]
symbols = table['Symbol']

tickers = symbols['Symbol'].tolist()

# ['AMC', 'ANZ', 'BHP', 'BXB', 'CBA', 'CSL', 'GMG', 'IAG', 'MQG', 'NAB', 'RIO', 'SCG', 'S32', 'SUN', 'TLS', 'TCL', 'WES', 'WBC', 'WPL', 'WOW']
print(tickers)
# <class 'list'>
print(type(tickers))

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