0

Here is my code. In which CROPS & CITY are lists contains names of crops and cities in the dataframe named df. I want to divide my single dataframe 'df' into multiple dataframes for each crops and city value. and want to store them in dataframes array for that what I should put in place of XYZ?

for crop in CROPS:
  for city in CITY:

    XYZ = df.loc[(df['Commodity.Name'] == 'crops') & (df['City.Name'] == 'city')]

1 Answer 1

1

Declare an empty list outside of the loop and keep on appending the filtered DataFrames to the list, like:

list_of_filtered_df = []
for crop in CROPS:
  for city in CITY:
    list_of_filtered_df.append(df.loc[(df['Commodity.Name'] == 'crops') & (df['City.Name'] == 'city')])

list_of_filtered contains the filtered DataFrames.

Sign up to request clarification or add additional context in comments.

1 Comment

Yes, I used same concept of dictionary just used dictionary[crop + '_' + city] = .....

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.