Reputation: 9
I've created a panda dataframe scraped from a website and have exported it into excel but the number values appear in text format in excel so wanted a quick way of converting all the number values into numbers that I can then analyse in excel automatically.
import requests
from bs4 import BeautifulSoup
import pandas as pd
from openpyxl import load_workbook
import csv
import os
def url_scraper(url):
response=requests.get(url)
html=response.text
soup=BeautifulSoup(html,"html.parser")
return soup
def first_inns_bowling_scorecard_scraper(url):
soup=url_scraper(url)
for divs in soup.find_all("div",{"id":"gp-inning-00"}):
for bowling_div in soup.find_all("div",{"class":"scorecard-section bowling"}):
table_headers=bowling_div.find_all("th")
table_rows=bowling_div.find_all("tr")[1:]
headers=[]
for th in table_headers:
headers.append(th.text)
data = []
for tr in table_rows:
td = tr.find_all('td')
row = [tr.text for tr in td]
data.append(row)
df=pd.DataFrame(data, columns=headers)
df.drop(df.columns[[1,9]], axis = 1,inplace=True)
df.to_excel(r'C:\\Users\\nathang\\Downloads\\random.xlsx',index = None, header=True)
os.chdir('C:\\Users\\nathang\\Downloads')
os.system("start EXCEL.EXE random.xlsx")
return df
url="https://www.espncricinfo.com/series/19781/scorecard/1216418/afghanistan-vs-ireland-3rd-t20i-ireland-tour-of-india-2019-20"
first_inns_bowling_scorecard_scraper(url)
I've tried multiple different variations of the df.apply(pd.to_numeric) on individual columns, multiple columns, the whole dataset and so on but can't get anything to work for it. Ideally, I would like to just input the whole dataframe into it and if there is an error it ignores it.
Upvotes: 0
Views: 44
Reputation: 66
This might solve your problem.
a = "5"
int(a) = 5
row = [int(tr.text) for tr in td]
Upvotes: 1