1
races = pd.read_csv("C:/Users/Sam/Documents/races.csv")
df_races = pd.DataFrame(races)
df_races = df_races[["raceId", "year", "name"]]
df_races = df_races.sort_values(by=['year'])
df_races = df_races[df_races['name'] == 'Australian Grand Prix']
# Australian Grand Prix 'Find Qualifying Data'
QLF = pd.read_csv("C:/Users/Sam/Documents/qualifying.csv")
df_QLF = pd.DataFrame(QLF)
df_QLF = df_QLF[["raceId", "position", "q1", "q2", "q3"]]
Race_Id_1 = df_races['raceId'].tolist()
# Filter Rows
df_QLF['Match'] = df_QLF["raceId"].isin(Race_Id_1)
def Find_Rid(row):
if row['Match'] == 'True':
    return row
df_QLF = df_QLF.apply(Find_Rid, axis=1)

print(df_QLF)

Once I run this all I get the following output, when actually all I want is when df_QLF['Match'] column == 'True' to keep these rows and discard all of the others

   0         None
   1         None
   2         None
   3         None
   ....      ....

I don't understand why.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • Can you please edit the code with the right indentation. Then, can you provide a data sample and the expected output. Thank you ! Have a look on [how to ask a good pandas question](https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples) – Alexandre B. Jul 02 '19 at 11:14

2 Answers2

1
df_QLF = df_QLF.loc[df_QLF['Match'] == True]
Greeser
  • 76
  • 3
  • Thanks. I'm going completely brain dead this afternoon! –  Jul 02 '19 at 11:26
  • Can I ask.. Why would you not use 'True' instead of True –  Jul 02 '19 at 11:27
  • Actually, I don't know for sure what you have: Boolean or String column type. Therefore, if you have a value 'True' as String inside, you may, of course, use `'True'` – Greeser Jul 02 '19 at 11:31
0

Following code worked for me. In python, True and False are specially reserved constants for bool class. Reference: 1) https://docs.python.org/2.3/whatsnew/section-bool.html 2) https://www.geeksforgeeks.org/bool-in-python/

def Find_Rid(row):
    if row['Match'] == True:
         return row
df_QLF_true = df_QLF.apply(Find_Rid, axis=1)
print(df_QLF_true)