1

I have a few datasets that share the same columns so I concatenated them together to form one large dateframe. My idea is to filter a goals_per_90 column by > .5 so it will create a new dataframe showing those whole rows of all the players with a value greater than .5 in a new dataframe. Im thinking of something like this at the moment but getting stuck when

 def gettopplayers(Dataframe):
   if Dataframe.loc[Dataframe['goals_per_90_overall'] > .5]:
     apply.

Im getting lost as to where to append this row to. Any help would be greatly appreciated. Thank you!

2
  • 1
    Adding minimal example with expected output may be helpful for others to answer. Commented Aug 21, 2020 at 20:02
  • If you are trying to create a new DataFrame by filtering rows of another Dataframe returning the df.loc[condition] will do the job Commented Aug 21, 2020 at 20:05

1 Answer 1

3

Below python code will make a new dataframe with all the rows where the condition is met. No need for the if condition.

df_new = Dataframe.loc[(Dataframe['goals_per_90_overall'] > .5)]
Sign up to request clarification or add additional context in comments.

2 Comments

Hi, can you explain what loc is doing here: [(Dataframe['goals_per_90_overall'] > .5)]? I am struggling to understand how to use loc but, I see it so much with pandas.
@TechNewbie The Dataframe['goals_per_90_overall'] > .5 produces a tuple of boolean values meeting the following condition. DF.loc() takes the tuple as input and returns the entire rows with corresponding index. loc is usually label based but can be used with booleans as in the above example. The best source for Pandas learning is the documentation pandas.pydata.org/pandas-docs/stable/reference/api/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.