【发布时间】:2019-02-02 22:30:47
【问题描述】:
我有一个 Python 脚本,用于组织来自网站的有关 NHL 球员的数据。这些值被放入数据框中。我还构建了一个函数,可以获取球员姓名和球队名称来获得球员阵容的总工资。我想将数据框中的球员姓名(F1、F2、F3)传递给函数(姓名)并将结果存储在我的 Excel 表(totalSalary)中。
我尝试将 iloc 函数传递给函数,但我很困惑。
from bs4 import BeautifulSoup
import requests
import pandas as pd
import colorama
import crayons
import datetime
import xlsxwriter
import nhl_player_salary as nps
def playerProductionData():
#Getting today's date
#today = str(datetime.date.today())
today = datetime.date.today().strftime("%m-%#d")
today = str(today).replace("-","/")
#print (today)
#Make it work on Windows machines
colorama.init()
# parameters for pandas display
def start():
options = {
'display': {
'max_columns': None,
'max_colwidth': 200,
'expand_frame_repr': False, # Don't wrap to multiple pages
'max_rows': 20,
'max_seq_items': 50, # Max length of printed sequence
'precision': 4,
'show_dimensions': False,
'colheader_justify': 'left'
},
'mode': {
'chained_assignment': None # Controls SettingWithCopyWarning
}
}
for category, option in options.items():
for op, value in option.items():
pd.set_option(f'{category}.{op}', value) # Python 3.6+
if __name__ == '__main__':
start()
del start # Clean up namespace in the interpreter
#Set Agent Header to scrape data
headers = {"User-Agent":'Mozilla/5.0 (Windows NT 6.3; WOW64)
AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Safari/537.36'}
page_link = 'https://www.leftwinglock.com/line-production/index.php?type=3'
#https://leftwinglock.com/articles.php?id=3049
page_response = requests.get(page_link, headers=headers, allow_redirects=False, timeout=5)
# here, we fetch the content from the url, using the requests library
page_content = BeautifulSoup(page_response.content, "html.parser")
#column_headers = page_content.findAll('tr')[0]
column_headers = [th.getText() for th in page_content.findAll('tr')[0].findAll('th')]
data_rows = page_content.findAll('tr')[1:]
player_data = [[td.getText() for td in data_rows[i].findAll('td', limit=14)] for i in range(len(data_rows))] #PLAYER DATA
#print (column_headers)
df = pd.DataFrame(player_data,columns=['Team', 'F1', 'F2', 'F3', 'GF', 'GA', 'GF%', 'SATF', 'SAT%', 'USATF', 'USAT%', 'SH%', 'SV%', 'SHSV%'])
#initilize total salary
df['TotalSalary'] = 0
#nps.getPlayerSalary(player_data.teamAbbrv)
#df['TotalSalary'] = nps.getPlayerSalary(df.iloc[:,0], ["ARVIDSSON","JOHANSEN", "FORSBERG"])
#print (df)
convert_fill(df)
df['SATF'] = df['SATF'].astype(int)
df['GF'] = df['GF'].astype(int)
#Create a Pandas Excel writer using XlsxWriter as the engine.
writer = pd.ExcelWriter('player_line_production_data.xlsx', engine='xlsxwriter')
# Convert the dataframe to an XlsxWriter Excel object.
df.to_excel(writer, sheet_name='Player Line Production Data')
# Close the Pandas Excel writer and output the Excel file.
writer.save()
return 'Run Successful'
def convert_fill(df):
return df.stack().apply(pd.to_numeric, errors='ignore').fillna(0).unstack()
print(playerProductionData())
import pandas as pd
from pandas import DataFrame
def getPlayerSalary(teamAbbrv, names):
#Get Most Recent Draft Kings Salary List
DKSalary = (pd.read_csv('DKSalaries.csv'))
DKSalary_DF = DataFrame(DKSalary, columns=['Position', 'Name', 'Salary', 'AvgPointsPerGame', 'TeamAbbrev'])
i = 0
def getDataFrameForNameTeam(teamAbbrv, name):
filterName = DKSalary_DF[DKSalary_DF['Name'].str.contains(name.title())]
filterName = filterName[filterName['TeamAbbrev'].str.contains(teamAbbrv)]
return filterName
nameDF = getDataFrameForNameTeam(teamAbbrv, names[0])
while i < len(names) - 1:
newframe = getDataFrameForNameTeam(teamAbbrv, names[i + 1])
nameDF = pd.concat([nameDF, newframe])
i += 1
return nameDF['Salary'].sum()
print (getPlayerSalary ('NSH', ["ARVIDSSON","JOHANSEN", "FORSBERG"]))
【问题讨论】:
-
请构造一个minimal reproducible example。如果您需要这方面的帮助,请阅读How to make good reproducible pandas examples。