728x90

 

Use DataFrame.query() method

df.query('arr_date <= dep_date.add(@pd.to_timedelta("7d"))')

Use scala expression 

df[ df["arr_date"]<=df["dep_date"]+timedelta(days=7) ]

Full code 

import pandas as pd
from datetime import date, timedelta


raw_data = {
    "arr_date" : [date(2022, 1, 12), date(2022, 1, 20), date(2022, 5, 2)],
    "dep_date" : [date(2022, 2, 1) , date(2022, 3, 1) , date(2022, 3, 1)],
    }

df = pd.DataFrame(data=raw_data)
#print(df.info())
print("org-data:")
print(df)

print("filtering-data : use scala-expression")
df2 = df[df["arr_date"]<=df["dep_date"]+timedelta(days=7)]
print(df2)

print("filtering-data : use query() method")
df3 = df.query('arr_date <= dep_date.add(@pd.to_timedelta("7d"))')
print(df3)

Output:

org-data:
     arr_date    dep_date
0  2022-01-12  2022-02-01
1  2022-01-20  2022-03-01
2  2022-05-02  2022-03-01

filtering-data : use scala-expression
     arr_date    dep_date
0  2022-01-12  2022-02-01
1  2022-01-20  2022-03-01

filtering-data : use query() method
     arr_date    dep_date
0  2022-01-12  2022-02-01
1  2022-01-20  2022-03-01
728x90
  • 네이버 블러그 공유하기
  • 네이버 밴드에 공유하기
  • 페이스북 공유하기
  • 카카오스토리 공유하기
반응형