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: