0

Here is my dataset:

enter image description here

I want to put in the column 'Solicitud de personacion' if its values are zero the values in the column 'Solicitud de personacion_'. I use a loop and the iloc function to index, but it doesn't work. What are the solutions?

hito = 'Solicitud personacion'
for i in range(len(a)):
    if a[hito].iloc[i] != 0:
        a[hito].iloc[i] = a[hito].iloc[i]
    else:
        a[hito].iloc[i] = a[hito + '_'].iloc[i]
mozway
  • 194,879
  • 13
  • 39
  • 75

1 Answers1

1

Use where:

df['Solicitud personacion'] = df['Solicitud de personacion_'].where(df['Solicitud personacion'].eq(0), df['Solicitud personacion'])
mozway
  • 194,879
  • 13
  • 39
  • 75
  • This sets the non-zero values of "Solicitud personacion" to 0, where they are expected to remain the same (according to the code they tried with). The second argument in `where` should be `df['Solicitud personacion']` instead of 0 – Uretki Aug 23 '21 at 07:37
  • Yes, sorry, I mixed up the condition (thought was already 0) – mozway Aug 23 '21 at 07:42