需要帮助将dataframe列转换为列表

时间:2020-09-06 21:30:56

标签: python dataframe

我正在尝试将所选表中的“符号”列转换为如下列表。 但是,它似乎不起作用。谁能解释为什么这不起作用?

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))

这是我得到的错误:

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

1 个答案:

答案 0 :(得分:0)

这是因为符号是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))