Filter/ Remove rows of value that dates fall on 2021 from given dataframe
Question:
Filter and remove dates that fall on 2021 from the given dataframe.
Example:
MNG Cus Regis_date
8059 281614530 20211230
8099 2810288123 20211105
8099 2810288123 20211106
8099 2810288123 20211210
8099 2810288123 20220110
8099 2810288123 20220124
8960 2815496326 20220422
Desired output:
MNG Cus Regis_date
8960 2815496326 20220422
Please help!
Thanks!
Answers:
Try this:
# list to store unique cus
cus: list = []
# Filter cus that have regis_date in 2021
for _, row in df.iterrows():
if row['Cus'] not in cus and row['Regis_date'].year == 2021:
cus.append(row['Cus'])
# Remove cus that have regis_date in 2021
df = df[df['Cus'].isin(cus) == False]
Filter and remove dates that fall on 2021 from the given dataframe.
Example:
MNG Cus Regis_date
8059 281614530 20211230
8099 2810288123 20211105
8099 2810288123 20211106
8099 2810288123 20211210
8099 2810288123 20220110
8099 2810288123 20220124
8960 2815496326 20220422
Desired output:
MNG Cus Regis_date
8960 2815496326 20220422
Please help!
Thanks!
Try this:
# list to store unique cus
cus: list = []
# Filter cus that have regis_date in 2021
for _, row in df.iterrows():
if row['Cus'] not in cus and row['Regis_date'].year == 2021:
cus.append(row['Cus'])
# Remove cus that have regis_date in 2021
df = df[df['Cus'].isin(cus) == False]