Runner Bean Runner Bean - 1 month ago 4
Python Question

Do something to every row on a pandas dataframe

Im trying to do something to a pandas dataframe as follows:

If say row 2 has a 'nan' value in the 'start' column, then I can replace all row entries with '999999'

if pd.isnull(dfSleep.ix[2,'start']):
dfSleep.ix[2,:] = 999999

The above code works but I want to do it for every row, ive tried replacing the '2' with a ':' but that does not work

if pd.isnull(dfSleep.ix[:,'start']):
dfSleep.ix[:,:] = 999999

and ive tried something like this

for row in df.iterrows():
if pd.isnull(dfSleep.ix[row,'start']):
dfSleep.ix[row,:] = 999999

but again no luck, any ideas?


I think row in your approach is not an row index. It's a row of the DataFrame

You can use this instead:

for row in df.iterrows():
    if pd.isnull(dfSleep.ix[row[0],'start']):
        dfSleep.ix[row[0],:] = 999999