如何按 pandas 中的日期边界从文本文件中选择行?

How do I choose lines from a text file by date boundaries in pandas?

我有这个输入文件: temp2.txt:

82.0    1   77  918     12/04/13        
47.0    1   77  9399    19/06/13        
39.0    1   77  4522    07/07/14        
52.0    1   77  504     13/05/14    

我只想将日期在 a 和 b 之间的行从 temp2.txt 复制到 output_file.txt

我写了这段代码:

import pandas as pd

    a = "01/01/2013"
    b = "31/12/2013"

    df = pd.read_csv('temp2.txt', header=None, sep='\s+')
    df['tmp'] = pd.to_datetime(df[4], errors='coerce')

    df['tmp'] = df['tmp'].between(a, b, inclusive=True)

    df.drop('tmp', axis=1, inplace=True)
    df.to_csv('output_file.txt', header=False, index=False, sep='\t')

但它似乎对我不起作用,我得到了所有的行,而不仅仅是我想要的 2 行。 我在这里做错了什么?

请用这个替换df.drop('tmp', axis=1, inplace=True)

df = df.loc[df['tmp']]

所以整个代码是:

import pandas as pd

a = "01/01/2013"
b = "31/12/2013"

df = pd.read_csv('temp2.txt', header=None, sep='\s+')
df['tmp'] = pd.to_datetime(df[4], errors='coerce')

df['tmp'] = df['tmp'].between(a, b, inclusive=True)

df = df.loc[df['tmp']]
df.to_csv('output_file.txt', header=False, index=False, sep='\t')