Python:将函数应用于数据框的多个子集(存储在字典中) [英] Python: Apply a function to multiple subsets of a dataframe (stored in a dictionary)

查看:60
本文介绍了Python:将函数应用于数据框的多个子集(存储在字典中)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此致

很抱歉,这个问题与其他问题重复.但是我可以找到一个能正确解决我的问题的答案.

Apologies if this question appears be to a duplicate of other questions. But I could find an answer that addresses my problem in its exactitude.

我将一个称为数据"的数据帧分为多个子集,这些子集存储在名为"dfs"的数据帧字典中,如下所示:

I split a dataframe, called "data", into multiple subsets that are stored in a dictionary of dataframes named "dfs" as follows:

# Partition DF

dfs = {}
chunk = 5

for n in range((data.shape[0] // chunk + 1)):
    df_temp = data.iloc[n*chunk:(n+1)*chunk]
    df_temp = df_temp.reset_index(drop=True)
    dfs[n] = df_temp

现在,我想将预定义的帮助函数"fun_c"应用于每个数据帧(存储在名为"dfs"的字典对象中).

Now, I would like to apply a pre-defined helper function called "fun_c" to EACH of the dataframes (that are stored in the dictionary object called "dfs").

一次将函数应用于dfs是否正确(如下?)?

Is it correct for me to apply the function to the dfs in one go, as follows(?):

result = fun_c(dfs)

如果没有,这样做的正确方法是什么?

If not, what would be the correct way of doing this?

推荐答案

这取决于您要查找的输出:

it depends on the output you're looking for:

  • 如果要在输出中使用字典,则应将函数应用于每个字典项
result = dict({key: fun_c(val) for key, val in dfs.items()})

  • 如果要在输出中列出数据框/值,则将函数应用于每个dict值
  • result = [fun_c(val) for val in dfs.items()]
    

    但是这种样式也不是错误的,您也可以在helper函数内部进行迭代:

    But this style isnt wrong either, you can iterate however you like inside the helper function as well:

    def fun_c(dfs):
    
        result = None
        # either
        for key, val in dfs.items():
            pass
        # or
        for val in dfs.values():
            pass
        return result
    
    

    让我知道这是否有帮助!

    Let me know if this helps!

    这篇关于Python:将函数应用于数据框的多个子集(存储在字典中)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆