0

The code below creates multiple empty dataframes named from the report2 list. They are then populated with a filtered existing dataframe called dfsource. With a nested for loop, I'd like to filter each of these dataframes using a list of values but the sub loop does not work as shown.

import pandas as pd

report=['A','B','C']
suffix='_US'

report2=[s + suffix for s in report]
print (report2) #result: ['A_US', 'B_US', 'C_US']

source = {'COL1': ['A','B','C'], 'COL2': ['D','E','F']}
dfsource=pd.DataFrame(source)
print(dfsource)

df_dict = {}
for i in report2:
    df_dict[i]=pd.DataFrame()

    for x in report:
      df_dict[i]=dfsource.query('COL1==x')
      #df_dict[i]=dfsource.query('COL1=="A"') #Example, this works filtering for value A but not what I need.

print(df_dict['A_US'])
print(df_dict['B_US'])
print(df_dict['C_US'])
1
  • 1
    What does your expected output look like? Commented Jun 2, 2021 at 21:03

1 Answer 1

1

You can reference a variable in a query by using @

df_dict[i]=dfsource.query('COL1==@x')

So the total code looks like this

import pandas as pd

report=['A','B','C']
suffix='_US'

report2=[s + suffix for s in report]
print (report2) #result: ['A_US', 'B_US', 'C_US']

source = {'COL1': ['A','B','C'], 'COL2': ['D','E','F']}
dfsource=pd.DataFrame(source)
print(dfsource)

df_dict = {}
for i in report2:
    df_dict[i]=pd.DataFrame()


    for x in report:
      df_dict[i]=dfsource.query('COL1==@x')
      #df_dict[i]=dfsource.query('COL1=="A"') #Example, this works filtering for value A but not what I need.

print(df_dict['A_US'])
print(df_dict['B_US'])
print(df_dict['C_US'])

which outputs

  COL1 COL2
0    A    D
1    B    E
2    C    F
  COL1 COL2
2    C    F
  COL1 COL2
2    C    F
  COL1 COL2
2    C    F

However, I think you want to create a new dictionary based on the i and x of each list, then you can move the creation of the dataframe to the second for loop and then create a new key for each iteration.

import pandas as pd

report=['A','B','C']
suffix='_US'

report2=[s + suffix for s in report]
print (report2) #result: ['A_US', 'B_US', 'C_US']

source = {'COL1': ['A','B','C'], 'COL2': ['D','E','F']}
dfsource=pd.DataFrame(source)
print(dfsource)

df_dict = {}
for i in report2:
    for x in report:
      new_key = x + i
      df_dict[new_key]=pd.DataFrame()
      df_dict[new_key]=dfsource.query('COL1==@x')

for item in df_dict.items():
    print(item)

Outputs 9 unique dataframes which are filtered based on whatever x value was passed.

('AA_US',   COL1 COL2
0    A    D)
('BA_US',   COL1 COL2
1    B    E)
('CA_US',   COL1 COL2
2    C    F)
('AB_US',   COL1 COL2
0    A    D)
('BB_US',   COL1 COL2
1    B    E)
('CB_US',   COL1 COL2
2    C    F)
('AC_US',   COL1 COL2
0    A    D)
('BC_US',   COL1 COL2
1    B    E)
('CC_US',   COL1 COL2
2    C    F)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.