【问题标题】:How to append multiple columns at the same time using panda for pythonpython - 如何使用panda for python同时附加多个列
【发布时间】:2019-07-30 20:41:31
【问题描述】:

我目前正在使用 python 在网络上抓取每个 NBA 球员的三分统计数据,并试图将这些数据放入数据框中。下面的代码是我尝试将值添加到数据框中。变量 player、teams、threePointAttempts 和 threePointPercentage 都是包含 50 个值的列表。由于脚本会在 NBA 网站的每个页面中移动,因此在每次 while 循环迭代后都会重新填充这些内容。

while i<10:
soup = BeautifulSoup(d.page_source, 'html.parser').find('table')
headers, [_, *data] = [i.text for i in soup.find_all('th')], [[i.text for i in b.find_all('td')] for b in soup.find_all('tr')]
final_data = [i for i in data if len(i) > 1]

data_attrs = [dict(zip(headers, i)) for i in final_data]
print(data_attrs)

players = [i['PLAYER'] for i in data_attrs]
teams = [i['TEAM'] for i in data_attrs]
threePointAttempts = [i['3PA'] for i in data_attrs]
threePointPercentage = [i['3P%'] for i in data_attrs]


data_df = data_df.append(pd.DataFrame(players, columns=['Player']),ignore_index=True)
data_df = data_df.append(pd.DataFrame(teams, columns=['Team']),ignore_index=True)
data_df = data_df.append(pd.DataFrame(threePointAttempts, columns=['3PA']),ignore_index=True)
data_df = data_df.append(pd.DataFrame(threePointPercentage, columns=['3P%']),ignore_index=True)
data_df = data_df[['Player','Team','3PA','3P%']]

我遇到的问题是数据框填充如下:

First columnSecond columnThird column

【问题讨论】:

    标签: python pandas dataframe web-scraping


    【解决方案1】:

    试试:

    temp_df = pd.DataFrame({'Player': players,
                            'Team': teams,
                            '3PA': threePointAttempts,
                            '3P%': threePointPercentage})
    
    data_df = data_df.append(temp_df, ignore_index=True)
    
    

    【讨论】:

    • 感谢您的回复。您发布的代码中有错误。它应该是 data_df = data_df.append(temp_df, ignore_index=True)。否则,它对我来说非常有用。
    • @BrennanMosher 你是对的,我很高兴能帮上忙 :)
    • @BrennanMosher,欢迎来到 SO。当针对您的问题发布有效/适当的解决方案时,请确保您接受答案。
    猜你喜欢
    • 2017-11-19
    • 2018-08-31
    • 2019-04-05
    • 2018-10-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-07
    相关资源
    最近更新 更多