Python 根据条件和位置设置值
我需要根据条件设置列的值,但我只需要在前5行设置它 这是我选择正确行的语句,它可以很好地工作Python 根据条件和位置设置值,python,pandas,Python,Pandas,我需要根据条件设置列的值,但我只需要在前5行设置它 这是我选择正确行的语句,它可以很好地工作 kunden_df.loc[kunden_df["Distanz"] == 1].iloc[0:amount_contracts] 但是,在列(Betreuer)上设置值并没有任何作用 kunden_df.loc[kunden_df["Distanz"] == 1].iloc[0:amount_contracts]["Betreuer"] =
kunden_df.loc[kunden_df["Distanz"] == 1].iloc[0:amount_contracts]
但是,在列(Betreuer)上设置值并没有任何作用
kunden_df.loc[kunden_df["Distanz"] == 1].iloc[0:amount_contracts]["Betreuer"] = value
我知道这是一个语法问题,我已经发现了非常类似的问题(比如:),但我不能让它工作。您能提供帮助吗?一种解决方案是确定要更改的行的索引,然后使用这些索引使用pandas函数设置新值 例如:
idx = kunden_df.loc[kunden_df["Distanz"] == 1].iloc[0:amount_contracts].index.values
kunden_df.at[idx, 'Betreuer'] = value