我使用res.filter(i=>Object.keys(i.ABC_Info.ABC).length >0);
作为异常处理程序在pandas数据帧上创建了基于txt的关键字提取器,但是代码似乎很长。这是我的数据集
other
这是名为id description
1 description: kartu debit 20/10 indomaretcipete r
4 description: biaya adm
15 description: tarikan atm 14/10
20 description: trsf ws269b100420/home credit 0372540
22 description: kartu debit 09/10 starbuckspasaraya
的txt文件
text.txt
这是我的代码
indomaret
starbucks
home credit
这是输出
with open('text.txt') as f:
content = f.readlines()
content = [x.strip() for x in content ]
def ambil(inp):
try:
out = []
for x in content:
if x in inp:
out.append(x)
if len(out) == 0:
return 'other'
else:
output = ' '.join(out)
return output
except:
return 'other'
df['keyword'] = df['description'].apply(ambil)
使用现有的pandas函数,我希望将代码缩短一点,怎么办日期呢?
答案 0 :(得分:1)
这应该有效,
df['keyword'] = df['description'].apply(lambda x: ' '.join([i for i in content if i in x]))
df['keyword'].fillna('other', inplace=True)