0

I have a dataframe where every row may have different numbers of columns. example of my df

    column1 column2 column3 column4 column5 column6
row1    2   4   5           
row2    3   4   5   6       
row3    1   2   4   6       
row4    2   3   5   7   8   
row5    1   2   4   5   6   
row6    2   3   4   7   8   9
row7    2   3   5   6   7   9
row8    3   4   6   8   9   
row9    2   4   5   7   9   10
row10   4   7   6   9       
row11   3   6   7   8   10  9
row12   4   6   8   9   11  
row13   4   5   7   10  9   11
row14   5   6   9   11  10  12

For every row, how can I loop through every column that has a value with pandas?

Something like:

for index, row in df.iterrows():
     #for every column that has a value
          #do something with this column value of this row

Thanks

Nicolas Gervais
  • 33,817
  • 13
  • 115
  • 143
marcus
  • 47
  • 10
  • 1
    dont iterate over a dataframe whenever possible , check this out for reference how you can assign values conditionally https://stackoverflow.com/questions/19913659/pandas-conditional-creation-of-a-series-dataframe-column – anky Mar 22 '20 at 17:22
  • what's a value, anything that's not a blank string? – Umar.H Mar 22 '20 at 17:25
  • In this case, i'm referring to value as a string or number – marcus Mar 22 '20 at 17:26

1 Answers1

0

It's bad practice, but you can just make another for loop with a mask that removes nan values. You were almost there:

for index, row in df.iterrows():
    for value in row[row.notnull()]:
        print(value)

It's bad practice because pandas supports vectorized operations, so there's almost always a way to do your operation without a loop.

Nicolas Gervais
  • 33,817
  • 13
  • 115
  • 143
  • you could just replace the blank strings with `np.nan` then use `isnull()` and specify the axis and use`.loc` for any boolean assignment – Umar.H Mar 22 '20 at 22:40