简体   繁体   English

根据条件应用 function 的最有效方法

[英]Most efficient way of applying a function based on condition

Suppose we have a master dictionary master_dict = {"a": df1, "b": df2, "c": df3} .假设我们有一个主字典master_dict = {"a": df1, "b": df2, "c": df3} Now suppose we have a list called condition_list .现在假设我们有一个名为condition_list的列表。 Suppose func is a function that returns a new dictionary that has the original keys of master_dict along with potentially new keys.假设func是一个 function,它返回一个新字典,该字典具有master_dict的原始键以及潜在的新键。

What is the best way to get the below code to work when the length of condition_list is greater than 2:condition_list的长度大于 2 时,让下面的代码工作的最佳方法是什么:

if(len(condition_list) == 1):
   df = master_dict[condition_list[0]] 
else:
   df = func(master_dict(condition_list[0]))
   df = df[condition_list[1]]

You need to ask clearly.你要问清楚。 Declare input and output. And try to make a demo code.声明输入和output。并尝试制作一个演示代码。 Anyway, use a loop.无论如何,使用循环。

for i in range(len(condition_list)):
    if i==0: df = master_dict[condition_list[i]]
    else:    df = func(df)[condition_list[i]];

If the "df" is a dataframe of pandas, the conditions can be applied at once.如果“df”是 pandas 的 dataframe,则可以立即应用条件。 Search "select dataframe with multiple conditions"搜索“多条件选择dataframe”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将繁重的计算功能应用于dask数据帧的最有效方法? - Most efficient way of applying a compute-heavy function to a dask dataframe? 基于满足条件的比较版本字符串的最Pythonic高效方法 - Most Pythonic and Efficient way to compare Version Strings based on meeting a condition 熊猫中基于两个条件的分组和聚集的最有效方法 - most efficient way to groupby and aggregate based on two condition in pandas 这是根据 pandas 中的条件删除 DataFrame 行的最有效方法? - which is the most efficient way to remove DataFrame rows based on a condition in pandas? 在 Pandas/Python 中以最有效的方式根据条件复制列的最后看到的非空值 - Copy the last seen non empty value of a column based on a condition in most efficient way in Pandas/Python Pandas:最节省资源的申请方式 function - Pandas: Most resource efficient way to apply function 将多个参数传递给函数的最有效方法? - Most efficient way to pass multiple arguments to a function? 在熊猫数据帧系列上应用基于条件的函数 - Applying function based on condition on pandas dataframe series 根据条件将 function 应用于 dataframe 中的列 - Applying a function to columns within a dataframe based on a condition 根据条件更新数据框列的有效方法 - efficient way to update dataframe column based on condition
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM