2

I have a dictionary of dataframes

list_of_dfs={'df1:Dataframe','df2:Dataframe','df3:Dataframe','df4:Dataframe'}

Each data frame contains the same variables (price, volume, price,"Sell/Purchase") that I want to manipulate to end up with a new subset of DataFrames. My new dataframes have to filter the variable called "Sell/Purchase" by the observations that have "Sell" in the variable.

sell=df[df["Sale/Purchase"]=="Sell"]

My question is how do I loop over the dictionary in order to get a new dictionary with this new subset?

I dont know how to write this command to do the loop. I know it has to start like this:

 # Create an empty dictionary called new_dfs to hold the results
new_dfs = {}
# Loop over key-value pair
for key, df in list_of_dfs.items():

But then due to my small knowledge of looping over a dictionary of dataframes I dont know how to write the filter command. I would be really thankful if someone can help me.

Thanks in advance.

2
  • 1
    This list_of_dfs is a set, not a list or dictionary. Commented Oct 17, 2019 at 12:12
  • new_dfs = {k: df[df["Sale/Purchase"]=="Sell"] for k, df in list_of_dfs.items()}. Assuming you miswrote the quotes'df1': df1 Commented Oct 17, 2019 at 12:15

3 Answers 3

1

Try this,

dict_of_dfs={'df1':'Dataframe','df2':'Dataframe','df3':'Dataframe','df4':'Dataframe'}


# Create an empty dictionary called new_dfs to hold the results
new_dfs = {}
# Loop over key-value pair
for key, df in dict_of_dfs.items():
    new_dfs[key] = df[df["Sale/Purchase"]=="Sell"]

Explanation:

new_dfs = {} # Here we have created a empty dictionary.
# dictionary contains keys and values.
# to add keys and values to our dictionary, 
# we need to do it as shown below,
new_dfs[our_key_1] = our_value_2
new_dfs[our_key_2] = our_value_2
.
.
.
Sign up to request clarification or add additional context in comments.

1 Comment

Thank. Could you please explain me what new_dfs[key] means? how does python undestand that this will filter the dataframes that are located in values for the dictionary if you are specifying it belongs to the key part?
0

You can map a function:

lambda df: df[df["Sale/Purchase"] == "Sell"]

HOW:

Syntax = map(fun, iter)

map(lambda df: df[df["Sale/Purchase"] == "Sell"], list_of_dfs)

You can map it on the a list, or set

For dict:

df_dict = {k: df[df["Sale/Purchase"]=="Sell"] for k, df in list_of_dfs.items()}

Comments

0

Something like:

sells = {k: v for (k, v) in list_of_df.items() if v["Sale/Purchase"] == "Sell"}

This pattern is called dictionary comprehension. According to this question this is the fastest and most Pythonic approach.

You should provide an example of the data you are dealing with for more precise answer.

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.