如何创建用户函数以'FALSE'
在列值字符串长度小于5时返回并在长度等于5时返回原始字符串值?
def z(row):
if df['column_val'].str.len() < 5:
return 'FALSE'
else:
return df['column_val']
当我尝试使用此方法创建列时出现此错误:
ValueError: ('The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().', 'occurred at index 0')
df['new_row'] = df['column_val'].apply(lambda x: 'FALSE' if len(x) < 5 else x)
应该给您答案,作为分配给的系列df['new_row']
。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句