0

I need to set the value of a column based on a condition, but I only need to set it on the first 5 rows.

This is my statement for selecting the correct rows which works fine.

kunden_df.loc[kunden_df["Distanz"] == 1].iloc[0:amount_contracts]

However setting the value on the column (Betreuer) does not do a thing.

kunden_df.loc[kunden_df["Distanz"] == 1].iloc[0:amount_contracts]["Betreuer"] = value

I know this is a syntax problem and I have already found quite similar questions (like: Pandas/Python: Set value of one column based on value in another column) , but I cant make it work. Can you help?

MLAlex
  • 142
  • 10

1 Answers1

1

One solution is to determine the indices of the rows you want to change and then set the new value with pandas at function using these indices

For example:

idx = kunden_df.loc[kunden_df["Distanz"] == 1].iloc[0:amount_contracts].index.values

kunden_df.at[idx, 'Betreuer'] = value
Jannik
  • 965
  • 2
  • 12
  • 21