gpt4 book ai didi

Python---使用数据帧循环字典

转载 作者:太空宇宙 更新时间:2023-11-03 20:16:10 24 4
gpt4 key购买 nike

我有一个数据帧字典

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

每个数据帧都包含相同的变量(价格、数量、价格、“销售/购买”),我想对其进行操作以最终得到一个新的数据帧子集。我的新数据框必须通过变量中包含“销售”的观察来过滤名为“销售/购买”的变量。

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

我的问题是如何循环字典以获得包含这个新子集的新字典?

我不知道如何编写这个命令来执行循环。我知道它必须这样开始:

 # 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():

但是由于我对数据帧字典循环的了解很少,我不知道如何编写过滤器命令。如果有人能帮助我,我将非常感激。

提前致谢。

最佳答案

试试这个,

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"]
<小时/>

说明:

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
.
.
.

关于Python---使用数据帧循环字典,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58432361/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com