我有一个包含两列的表格。我必须从Notes列中提取出有意义的信息。即我需要在一列中提取日期,在一列中提取日期后的信息,然后提取ID
Notes, ID
Movie Date 05-28-2018 Passed, 1010
MTD loan slip dated 8-10-14 the Issued, 1111
Max over date 10-2-15 and repaired, 11232
输出 -
Notes ID Date Status
Movie Date 05-28-2018 Passed 1010 5/28/2018 Passed
loan slip dated 8-10-14 Issued 1111 8/10/2014 Issued
Max over date 10-2-15 and repaired 11232 10/2/2015 repaired
这是我的代码 -
df = pd.read_sql('select * from <table>', engine)
searchfor = [' dated', ' date', ' Date', ' Dated']
df2 = df[df['Notes'] .str.contains('|'.join(searchfor), na = False)]
..................
感谢您对此的帮助。谢谢。
答案 0 :(得分:0)
我会为此做一些循环。
示例强>:
import pandas as pd
df = pd.read_csv("data.csv")
searchforstatus = [' Passed', ' Issued', ' repaired']
for idx, row in df.iterrows():
for c in searchforstatus:
if c in row['Notes']:
df.loc[idx, 'Status'] = c
<强>结果强>
Notes ID Status
0 Movie Date 05-28-2018 Passed 1010 Passed
1 MTD loan slip dated 8-10-14 the Issued 1111 Issued
2 Max over date 10-2-15 and repaired 11232 repaired
我使用的数据可以在这里找到: https://files.fm/u/npaceyd6#_
答案 1 :(得分:0)
从iterrows()获取行后的正则表达式也可以提取信息,如果有多种可能性
s = 'Movie Date 05-28-2018 Passed'
p = re.search(r'Dated?\s(\d+-\d+-\d+)\s([a-zA-Z]+)',s)
p.group(1)将具有日期值,p.group(2)将具有值“Passed”。希望这会有所帮助..