Getting the pandas mean/meduim/mode to work, using variable value vs. explicitly defining them

What error does your original give you?


formulaUsed = df.loc[df.Team == team].rowoneData.mean() ^ SyntaxError: invalid syntax

AttributeError: 'DataFrame' object has no attribute 'rowoneData'

The error makes sense, since there is really no column named "rowoneData" in the dataframe.

But the variable 'rowoneData' has a stored value, that I want to be used and that is where I am getting stuck. Going back to what I had mentioned:

explicit (This works and returns the mean of column RS, where the team equals "ARI") formulaUsed = df.loc[df.Team == "ARI"].RS.mean()

I want to do this instead: team="ARI" rowoneData= "RS" df.loc[df.Team == team].rowoneData.mean()

I tried doing this: team="ARI" rowoneData= "RS" f"df.loc[df.Team == \"{team}\"].{rowoneData}.mean()"

but this will only get me the string (df.loc[df.Team == "ARI"].RS.mean()) and won't actually execute this line, to perform the calculation

I actually found the solution. In order for me to do what I wanted to do, I had to use the following:

team="ARI" rowoneData= "RS

df.loc[(df.Team == team, rowoneData)].mean()

Thanks for sharing!