我已经创建了一个从网站抓取的熊猫数据框,并将其导出到excel,但是数字值在excel中以文本格式显示,因此希望有一种快速的方法将所有数字值转换为可以在excel中进行分析的数字自动。
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)
我在单个列,多个列,整个数据集等上尝试了df.apply(pd.to_numeric)的多个不同变体,但没有任何可用的方法。理想情况下,我只想将整个数据帧输入其中,如果有错误,它将忽略它。
答案 0 :(得分:0)
这可能会解决您的问题。
a = "5"
int(a) = 5
row = [int(tr.text) for tr in td]