【发布时间】:2018-12-07 09:01:35
【问题描述】:
我正在创建一个示例数据框:
tp = pd.DataFrame({'source':['a','s','f'],
'target':['b','n','m'],
'count':[0,8,4]})
并根据'target'列的条件创建'col'列>>与源相同,如果匹配条件,否则为默认值,如下:
tp['col'] = tp.apply(lambda row:row['source'] if row['target'] in ['b','n'] else 'x')
但它向我抛出了这个错误:KeyError: ('target', 'occurred at index count')
如何在不定义函数的情况下使其工作?
【问题讨论】:
标签: python pandas dataframe if-statement lambda