0

I am trying to iterate a Pandas DataFrame (row-by-row) that has non-sequential index labels. In other words, one Dataframe's index labels look like this: 2,3,4,5,6,7,8,9,11,12,.... There is no row/index label 10. I would like to iterate the DataFrame to update/edit certain values in each row based on a condition since I am reading Excel sheets (which has merged cells) into DataFrames.

I tried the following code (@ Manuel's answer) to iterate through each row of df and edit each row if conditions apply.

    for col in list(df): #All columns 
       for row in df[1].iterrows(): ##All rows, except first   
           if pd.isnull(df.loc[row[0],'Album_Name']): ##If this cell is empty all in the same row too.
               continue  
           elif pd.isnull(df.loc[row[0], col]) and pd.isnull(df.loc[row[0]+1, col]):  ##If a cell and next one are empty, take previous value.
               df.loc[row[0], col] = df.loc[row[0]-1, col]  

However, since the DataFrame has non-sequential index labels, I get the following error message: KeyError: the label [10] is not in the [index]. How can I iterate and edit the DataFrame (row-by-row) with non-sequential index labels?

For reference, here is what my Excel sheet and DataFrame looks like:

Side by side capture

CPU
  • 267
  • 1
  • 6
  • 16

1 Answers1

0

Yes, just change the second loop to:

for row in df:

and then refer to the row with "row", not name.

 for col in df: #All columns 
       for row in df: ##All rows, except first   
           if row==1:
                continue #this skips to next loop iteration
           if pd.isnull(df.loc[row[0],'Album_Name']): ##If this cell is empty all in the same row too.
               continue  
           elif pd.isnull(df.loc[row[0], col]) and pd.isnull(df.loc[row[0]+1, col]):  ##If a cell and next one are empty, take previous value.
               df.loc[row[0], col] = df.loc[row[0]-1, col]  
EHB
  • 1,127
  • 3
  • 13
  • 24
  • If this doesn't work, make a reproducible example and that will make your problem more clear. Good instructions on how to do that are here: https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples – EHB Dec 15 '17 at 22:34
  • @ EHB - I am confused with the indentation in your code. Does the first `continue` align with the second `if` loop? – CPU Dec 15 '17 at 22:49