1 回答
TA贡献1844条经验 获得超8个赞
在执行 时df['Klasse'] == 'value',value被分配给整个列,因为分配是矢量化操作。您可以使用locpandas 中的 dataframe 方法并传递行和列的正确索引名称,以重新分配值,如下所示:
def Klasse(df):
for index, value in df.Opnameduur.items():
if value == 1:
df.loc[index, 'Klasse'] = 'dag'
elif value == 2:
df.loc[index, 'Klasse'] = 'kort'
elif value == 3:
df.loc[index, 'Klasse'] = 'kort'
elif value == 4:
df.loc[index, 'Klasse'] = 'middel'
elif value == 5:
df.loc[index, 'Klasse'] = 'middel'
elif value == 6:
df.loc[index, 'Klasse'] = 'middel'
elif value >= 7:
df.loc[index, 'Klasse'] = 'lang'
else:
df.loc[index, 'Klasse'] = 'onbekend'
添加回答
举报