我最近捡起了python,所以请原谅我的无知。 我正在根据匹配条件创建一个新列。如果搜索条件匹配,则打印搜索文本,否则在搜索文本为null时(“ []”)打印未分类的内容。
d = pd.read_csv('crime.csv')
df=pd.DataFrame(d)
search = "BURGLARY|PETTY"
df["Attributes"]= df["crimedescr"].str.findall(search)
df.head(10)
<table><tbody><tr><th>crimedescr</th><th>Attributes</th></tr><tr><td>10851(A)VC TAKE VEH W/O OWNER</td><td>[]</td></tr><tr><td>459 PC BURGLARY RESIDENCE</td><td>[BURGLARY]</td></tr><tr><td>10851(A)VC TAKE VEH W/O OWNER</td><td>[]</td></tr><tr><td>476 PC PASS FICTICIOUS CHECK</td><td>[]</td></tr><tr><td>459 PC BURGLARY-UNSPECIFIED</td><td>[BURGLARY]</td></tr><tr><td>530.5 PC USE PERSONAL ID INFO</td><td>[]</td></tr><tr><td>459 PC BURGLARY VEHICLE</td><td>[BURGLARY]</td></tr><tr><td>484 PC PETTY THEFT/INSIDE</td><td>[PETTY]</td></tr><tr><td>459 PC BURGLARY BUSINESS</td><td>[BURGLARY]</td></tr><tr><td>1708 US THEFT OF MAIL</td><td>[]</td></tr></tbody></table>