0
def recursiveIDidentifier(i, df):
    for j in range(len(df)):
        if df.iloc[j,i] == 0:
            # print('Zeile: ',row[0],'ist null in Column: ', row[i])
            recursiveIDidentifier(i-1, df)
            return

        else:
            df.loc[j,'id'] = df.iloc[j,i]
            i = 8
            print('Gefunden')
            return

i=8
recursiveIDidentifier(i, df)
0     4035  100834       0     0  0  0  0  0       0
1     4035  100834  100704     0  0  0  0  0       0
2     4035  100834  100943     0  0  0  0  0       0
3     4035  100834  100843     0  0  0  0  0       0
4     4035  100834  100841     0  0  0  0  0       0

Hey there. I am trying to find the Id which is not zero and write it into the last column which is not shown here. There might be a little mistake with finishing each run of the recursion before going to the next row. Any ideas? I tried different things with return and so on. But if i use return to finish each call of the recursion, i will finish the whole loop already in the first row.

yannickhau
  • 385
  • 1
  • 13

1 Answers1

1

You could solve your problem with one line: df['id'] = df.apply(lambda x: next((e for e in x[::-1] if e != 0), 0), axis=1)

Mark
  • 532
  • 2
  • 6