Square9627 Square9627 - 5 months ago 9
Python Question

Python - IF for a column in panda datafram

I would like to turn

NaN
values in the column
GP
in the dataframe
draft
(which has 22 rows) into
0
. I tried these lines but they dont work

import pandas as pd
draft = pd.read_csv('Draft year.csv')

if draft['GP'].isnull():
draft['GP'] = 0


In R there is
ifelse
which does the job nicely but I dont know equivalent in python. Really appreciate any help

Answer

You can use list comprehension and replace the NaNs with zeros.

draft['GP'] = [0 if np.isnan(x) else x for x in draft.GP]
Comments