'How do I split a pandas dataframe column into 3 unique columns?
I have a dataframe with college basketball betting odds. I need to split the first column 'game' into 'time', 'home', 'away'. The home team is the last team listed in the 'game' dataframe.
The data is being scraped into a data frame via beautiful soup.
import pandas as pd # library for data analysis
import numpy as np
import requests # library to handle requests
from bs4 import BeautifulSoup # library to parse HTML documents
url = "https://vegasinsider.com/college-basketball/odds/las-vegas/"
response=requests.get(url)
print(response.status_code)
soup = BeautifulSoup(response.text, 'html.parser')
indiatable=soup.find('table',{'class':"frodds-data-tbl"})
df=pd.read_html(str(indiatable))
# convert list to dataframe
df=pd.DataFrame(df[0])
print(df.head())
df.columns =['game', 'open','consensus','betmgm','caesars','fanduel','draftkings','pointsbet','wynn','superbook']
df
I need help splitting the first column into three columns though. This is the code I am using.
df[['time', 'home', 'away']] = df['game'].str.split(expand=True)
I need to new data frame to look like:
df = pd.DataFrame ({'time': ['02/02 7:00 PM'], 'home': ['Furman'], 'away': ['The Citadel']})
Per requests, the output of df[['game']].to_dict() is:
{'game': {0: '02/02 7:00 PM 665\xa0The Citadel 666\xa0Furman'}}
Thank you in advance!
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|


