Python如果存在YYYY,则保留行,否则删除该行

时间:2018-08-05 12:37:46

标签: python regex date dataframe lambda

我有一个带有日期列的数据框,我想从日期列中删除那些没有YYYY(例如,2018,可以是任何年份)格式的行。 我已经使用了带有正则表达式的apply方法,但是不起作用,

df[df.Date.apply(lambda x: re.findall(r'[0-9]{4}', x))]

“日期”列的值可以为

12/3/2018
March 12, 2018
stackoverflow
Mar 12, 2018
no date text
3/12/2018

所以这里的输出应该是

12/3/2018
March 12, 2018
Mar 12, 2018
3/12/2018

1 个答案:

答案 0 :(得分:2)

这是一种方法。将test1test2一起使用

例如:

    // set content offset at the center of the background
    let w = imageViews[0].frame.width
    let vw = view.frame.width
    backgroundScrollView.contentOffset.x = (w - vw)/2

    print ("\(#function): Background image view = \(imageViews[0].frame), start offset = \(backgroundScrollView.contentOffset.x), view  = \(view.frame)")

    let duration: TimeInterval = 10.0
    let speed = TimeInterval(w + vw) / duration
    let halfDuration = TimeInterval(w / 2 + vw) / speed

     UIView.animate(withDuration: halfDuration, delay: 0, options: [ .curveEaseIn ], animations: {
        self.backgroundScrollView.contentOffset.x =  w
     }){ completed in
        self.backgroundScrollView.contentOffset.x = 0

        UIView.animate(withDuration: duration, delay: 0, options: [ .repeat, .curveLinear ], animations: {
            self.backgroundScrollView.contentOffset.x = w
        }) { completed in
        }
    }

输出:

pd.to_datetime

或者如果您想保留原始数据

errors="coerce"

输出:

import pandas as pd
df = pd.DataFrame({"Col1": ['12/3/2018', 'March 12, 2018', 'stackoverflow', 'Mar 12, 2018', 'no date text', '3/12/2018']})
df["Col1"] = pd.to_datetime(df["Col1"], errors="coerce")
df = df[df["Col1"].notnull()]
print(df)