user11357465
user11357465

Reputation:

How do you apply a function to a dataframe column

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.

Upvotes: 1

Views: 52

Answers (2)

Durgesh Kumar
Durgesh Kumar

Reputation: 58

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)

Upvotes: 0

Greeser
Greeser

Reputation: 76

df_QLF = df_QLF.loc[df_QLF['Match'] == True]

Upvotes: 1

Related Questions