0

I have a huge dataset, which contains a 'Player' column. I want to create a function whereby I pass a value in as a parameter, and the function creates a dataframe slice which only contains rows which have a 'Player' value of whatever I have passed into the function. I want to be able to use this dataframe slice to then build some plots.

I really am a python novice so am unsure where to start. This is my first stab, any help would be much appreciated!

historics = pd.read_csv('historicdivs.csv')
def playerslice(name):
    is_player = historics['Player'] == 'name(as in the 
    parameter)'
    name(as in the parameter)_divs = historics[is_player]
    return name_divs

Let me know if I haven't been clear, I'm new to the site.

Cheers.

Chris
  • 15,819
  • 3
  • 24
  • 37
Mullins
  • 67
  • 9
  • 1
    To be fair, I think it would be good for your own development to do some basic pandas tutorials, because this is one of the first basics you will learn. Second, if you want to create a plot for each unique player, you probably want `GroupBy`, so also have a look into that. Third, it is strongly discouraged to convert a string to variable. – Erfan Jun 11 '20 at 21:20
  • `return historics[historics['Player'] == name]` – Chris Jun 11 '20 at 21:20
  • @Erfan cool, i was fearful this question could be painfully basic for this platform! When you say it is strongly discouraged to convert a string to a variable, do you mean that it's bad practise to take the string passed as a parameter and to use it as a variable name (e.g. name(as in the parameter)_divs)? If so, why? – Mullins Jun 11 '20 at 21:29
  • Yes, there's no reason in this case to do `name(as in the parameter)_divs = historics[is_player]`. You just want to return a value like the comment of Chris. If you want to read more about why, see [this](https://stackoverflow.com/questions/19122345/convert-string-to-variable-name-in-python) topic. – Erfan Jun 11 '20 at 21:30
  • @Erfan but I want to be able to use different outputs from this function in the same plot? As in I want to create a plot that uses data from two different data slices, which would both have been created using the function? If these new slices haven't been given different names by the function, I won't be able to do that? – Mullins Jun 11 '20 at 21:37

0 Answers0