Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pandas as pd
- import datetime
- # set current date
- current_date = datetime.datetime.now()
- # some data
- data = [[1, 'Yes','2019-08-15','2022-08-15','Yes'],
- [2, 'Yes','2019-08-15','2019-08-15','No'],
- [3, 'Yes','2019-08-15','2019-08-15','Yes'],
- [4, 'No','','','Yes'],
- [5, 'No','','2019-08-15','No'],
- [6, 'Yes','2020-08-15','2019-08-15','Yes'],
- [7, 'Yes','2019-08-15','2019-08-15','No'],
- [8, 'Yes','2019-08-15','2019-08-15','Yes'],
- [9, 'No','','2019-08-15','No'],
- [10, 'Yes','2019-08-15','2019-08-15','Yes']]
- # create dataframe
- df = pd.DataFrame(data,columns=['Cust_ID','OrderMade','OrderDate',
- 'ShipDate','OrderCategoryB'])
- # list of date columns
- date_cols = ["OrderDate","ShipDate"]
- # Convert all date columns to datetime
- for col in date_cols:
- df[col] = pd.to_datetime(df[col], errors ="coerce")
- """ Because of an additional requirement that is outside the scope of this question,
- date columns must be converted using a loop and not when creating the dataframe. """
- # create empty dataframe
- future_date_df = pd.DataFrame()
- # iterate and append rows where condition is true
- for col in date_cols:
- row = df.loc[((df[col].notna()) & (df[col] > current_date))]
- future_date_df = future_date_df.append(row)
- future_date_df['Column Name'] = col # this returns the last col in the
- # list and also throw "FutureWarning:
- # Sorting because non-concatenation
- # axis is not aligned."
Advertisement
Add Comment
Please, Sign In to add comment