简体   繁体   English

如何在 Python 中的 DataFrame 列表上快速应用函数?

[英]How to apply a function fastly on the list of DataFrame in Python?

I have a list of DataFrames with equal length of columns and rows but different values, such as data = [df1, df2,df3.... dfn] .我有一个列和行长度相等但值不同的 DataFrame 列表,例如 data = [df1, df2,df3.... dfn] 。 How can I apply a function function on each dataframe in the list data?如何在列表数据中的每个数据帧上应用函数函数? I used following code but it doe not work我使用了以下代码但它不起作用

data = [df1, def2,df3.... dfn]
    def maxloc(data):

        data['loc_max'] = np.zeros(len(data)) 

        for i in range(1,len(data)-1):  #from the second value on
            if data['q_value'][i] >= data['q_value'][i-1] and data['q_value'][i] >= data['q_value'][i+1]:
                data['loc_max'][i] = 1
        return data
    df_list = [df.pipe(maxloc) for df in data]

Seems to me the problem is in your maxloc() function as this code works.在我看来,问题出在您的 maxloc() 函数中,因为此代码有效。 I added also the maximum value in the return of maxloc.我还添加了 maxloc 返回的最大值。

from random import randrange
import pandas as pd


def maxloc(data_frame):
    max_index = data_frame['Value'].idxmax(0)
    maximum = data_frame['Value'][max_index]
    return max_index, maximum


# create test list of data-frames
data = []
for i in range(5):
    temp = []
    for j in range(10):
        temp.append(randrange(100))
    df = pd.DataFrame({'Value': temp}, index=(range(10)))
data.append(df)


df_list = [df.pipe(maxloc) for df in data]

for i, (index, value) in enumerate(df_list):
    print(f"Data-frame {i:02d}: maximum = {value} at position {index}")

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

相关问题 如何在 Python 3.7 中快速并行化 function? - How to Parallize the function fastly in Python 3.7? 将 function 应用于 dataframe 的每一行并将数据存储到 Python 中的列表/数据框 - Apply a function to every row of a dataframe and store the data to a list/Dataframe in Python 如何将函数应用于python中列表的每个子列表? - How to apply a function to each sublist of a list in python? 在熊猫数据框python上使用Apply函数时如何中断? - How to interrupt while using apply function on a pandas dataframe python? 如何在 python 中多次将自定义 function 应用于同一个 dataframe? - How to apply a custom function to the same dataframe multiple times in python? 如何在Python中将掩码应用于DataFrame? - How to apply a mask to a DataFrame in Python? 将 lambda function 应用到 pandas Z6A8064B5DF4794555500Z53C4C7D 中的列列表 - Apply lambda function to list of columns in pandas dataframe 将 function 应用于循环中的列列表和 output dataframe - Apply function to a list of columns in a loop and output dataframe Python将函数应用于DataFrame的每一行 - Python apply function to each row of DataFrame 如何在 python 中的二维数组/列表上应用过滤器 function - How to apply filter function on 2D array/list in python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM