简体   繁体   English

Function参数

[英]Function parameter

----Updated code--- ----更新代码---

I have problems with the following code我有以下代码的问题

df = pd.read_csv('Fifa20.csv')    
selector = df[['Name', 'Overall', 'Skill', 'Dribbling', 'Curve']]

def get_player(player):
    selector.loc[selector['Name'] == player]

    return selector[['Overall', 'Skill', 'Dribbling', 'Curve']].sum(axis=1) / len(selector.columns)

I was working a bit with the fifa20 dataset from kaggle, and wanted to make a dynamic function, which take the players name and then give me a number based on their skillset.我正在使用来自 kaggle 的 fifa20 数据集,并想制作一个动态的 function,它采用球员姓名,然后根据他们的技能组合给我一个数字。 I cannot figure out how to connect the two parts in my functions, so if I call my function with "Lionel Messi" it will return the calculated number.我不知道如何在我的函数中连接这两个部分,所以如果我用“Lionel Messi”调用我的 function,它将返回计算出的数字。

Could you guide me or give me a hint, to how I should think and connect the two?你能指导我或给我提示,让我知道我应该如何思考并将两者联系起来吗? At this moment, I think I am overthinking it quite a bit.此刻,我觉得我想得太多了。

Thank you!谢谢!

You need to include the dataframe as well as a function parameter.您需要包含 dataframe 以及 function 参数。 This should work fine-这应该可以正常工作-

df = pd.read_csv('Fifa20.csv')    
selector = df[['Name', 'Overall', 'Skill', 'Dribbling', 'Curve']]
player = 'Lionel Messi'

def get_player(player, selector):
    # Subsets selector to contain specific player's information
    selector = selector.loc[selector['Name'] == player]
    return selector[['Overall', 'Skill', 'Dribbling', 'Curve']].sum(axis=1) / len(selector.columns)

This function takes the player name and dataframe as input and returns the desired value.这个 function 将玩家名称和 dataframe 作为输入并返回所需的值。

Try this:试试这个:

Assuming you have loaded your dataset in selector dataframe. Then,假设您已将数据集加载到selector dataframe 中。然后,

  1. Select that row with the player name. Select 那一排玩家的名字。

  2. Apply sum on the skills feature and divide it by count of skills对技能特征应用求和并将其除以技能数量

    def get_player(player): global selector player_row = selector.loc[selector['Name'] == player] return player_row[['Overall', 'Skill', 'Dribbling', 'Curve']].sum(axis=1) / len(player_row.columns)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM