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
'Data Science > Python' 카테고리의 다른 글
[python] Deepcopy a list (array) (0) | 2022.09.01 |
---|---|
[python] Accelerate Requests Using asyncio (0) | 2022.09.01 |
[pandas]Dataframe Pivoting code. (0) | 2022.04.22 |
[Pandas] Database-style DataFrame joining/merging (0) | 2022.04.16 |
[pandas] Dataframe concatenate (0) | 2022.04.16 |
최근댓글