我已尝试在以下网站上找到未成功的答案:
Adding null values to a pandas dataframe
我需要通过python函数添加空值。
from numpy import nan
def f(row):
if row['model'] == 'C5' and row['x'] <= 600:
val = 600
elif row['model'] == 'C5' and row['x'] > 600 and row['score'] <= 675:
val = 675
elif row['model'] == 'C5' and row['x'] > 675 and row['score'] <= 710:
val = 710
elif row['model'] == 'C5' and row['x'] > 710 and row['score'] <= 745:
val = 745
elif row['CREDIT_MODEL_CODE'] == 'C5' and row['x'] > 745:
val = 999
elif row['model'] == 'C5' and row['x'] == 'X':
val = 3
elif row['model'] == 'C5' and row['x'] == 'T':
val = 2
elif row['model'] == "":
val = 1
return val
df['column'] = df.apply(f, axis = 1)
我还尝试使用NaN
和Null
代替""
。有谁知道我可以使用if-elif语句根据其他条件添加空值?
我一直收到以下错误:
48 49 else:
---> 50 val ==“” 51 52 return val
UnboundLocalError :(“之前引用的本地变量'val' 分配”,“发生在索引60”)