I have a pandas dataframe as below.I want to create list of columns by iterating over list called 'fields_list' and separate out lists which ends with the list in 'fields_list'
import pandas as pd
import numpy as np
import sys
df = pd.DataFrame({'a_balance': [3,4,5,6], 'b_balance': [5,1,1,1]})
df['ah_balance'] = 0
df['a_agg_balance'] = 0
df['b_agg_balance'] = 0
df
a_balance b_balance ah_balance a_agg_balance b_agg_balance
3 5 0 0 0
4 1 0 0 0
5 1 0 0 0
6 1 0 0 0
fields_list = [ ['<val>','_balance'],['<val_class>','_agg_balance']]
fields_list
[['<val>', '_balance'], ['<val_class>', '_agg_balance']]
for i,field in fields_list:
df_final= [col for col in df if col.endswith(field)]
print("df_final" ,df_final)
I tried above code but when it iterates over 1st element of fields_list(i.e. '', '_balance') it also includes elements that ends with '_agg_balance' and hence I get the below result
df_final ['a_balance', 'b_balance', 'ah_balance', 'a_agg_balance', 'b_agg_balance']
df_final ['a_agg_balance', 'b_agg_balance']
My expected output is
df_final ['a_balance', 'b_balance', 'ah_balance']
df_final ['a_agg_balance', 'b_agg_balance']