Home > Net >  How to select only rows containing specific values with multiple data frame in for loop?
How to select only rows containing specific values with multiple data frame in for loop?

Time:07-28

I'm new to python, I have a multiple data frame and select data frame based one columns which contains value xxx.

below is my code

MasterFiles = [Master_Jun22, Master_May22, Master_Apr22, Master_Mar22, Master_Feb22, Master_Jan22,
          Master_Dec21, Master_Nov21, Master_Oct21, Master_Sep21, Master_Aug21, Master_Jul21,
          Master_Jun21, Master_May21, Master_Apr21]
ColumName = ['product_category']

for d in MasterFiles:
    for c in ColumName:
        d = d.loc[d[c]=='XXX']

it is not working please help on this.

CodePudding user response:

You need to gather the output and append it to a new Dataframe:

MasterFiles = [Master_Jun22, Master_May22, Master_Apr22, Master_Mar22, Master_Feb22, Master_Jan22,
          Master_Dec21, Master_Nov21, Master_Oct21, Master_Sep21, Master_Aug21, Master_Jul21,
          Master_Jun21, Master_May21, Master_Apr21]
ColumName = ['product_category']

res_df = pandas.Dataframe(columns=ColumName)

for d in MasterFiles:
    for c in ColumName:
        res_df.append[d.loc[d[c]=='XXX']]

# the results
res_df.head()

CodePudding user response:

I am not sure if I am understanding your question correctly. So, please let me rephrase your question here.

You have 3 tasks,

  • first is to loop through each pandas data frame,
  • second is to loop through each column in your ColumName list, and
  • third is to return the data frame rows that consists of value Surabhi - DCL - Unsecured based on the column name in the columnName list.

If I am interpreting this correctly. This is how I would work on your issue.

MasterFiles = [Master_Jun22, Master_May22, Master_Apr22, Master_Mar22, Master_Feb22, Master_Jan22,
              Master_Dec21, Master_Nov21, Master_Oct21, Master_Sep21, Master_Aug21, Master_Jul21,
              Master_Jun21, Master_May21, Master_Apr21]
ColumName = ['product_category']

## list to store filter data frame by rows
df_temp = []

for d in MasterFiles:
    for c in ColumName:
        df_temp.append(d.loc[d[c] == 'Surabhi - DCL - Unsecured'])

## Assuming row wise concatenation
## i.e., using same column names to join data
df = pd.concat(df_temp, axis=0, ignore_index=True)
## df is the data frame you need
  • Related