简体   繁体   English

Pandas 将自定义 function 应用到每个 dataframe 结果和 Z9516DFB15F51C7EE19A4D46DZC0

[英]Pandas apply custom function to each dataframe row and append results

How can I apply a custom function to each row of a Pandas dataframe df1 , where:如何将自定义 function 应用于 Pandas dataframe df1的每一行,其中:

  1. the function uses values from a column in df1 function 使用df1中的列中的值
  2. the function uses values from another dataframe df2 function 使用来自另一个 dataframe df2的值
  3. the results are appended to df1 column-wise结果按列附加到df1

Example:例子:

df1 = pd.DataFrame([1, 2, 3], columns=["x"])

df2 = pd.DataFrame({"set1": [0, 0, 0, 0], "set2": [100, 200, 300, 400]})

display(df1, df2)

在此处输入图像描述

And custom function和定制function

def myfunc(df2, x=df1["x"]):
    # Something simple but custom
    ans = df2["set1"] + df2["set2"] * x
    return ans

Desired output is所需的 output 是

x X run1运行1 run2运行2 run3运行3 run4运行4
0 0 1 1 100 100 200 200 300 300 400 400
1 1 2 2 200 200 400 400 600 600 800 800
2 2 3 3 300 300 600 600 900 900 1200 1200

Here is an example function call;这是一个示例 function 调用; but how can I apply it with a oneliner to get the desired dataframe output?但是如何将它与 oneliner 一起应用以获得所需的 dataframe output?

test = myfunc(df2,x=3)
print(test)

在此处输入图像描述

If you really need a custom function, you can use apply :如果你真的需要一个定制的 function,你可以使用apply

# Modified slightly to make using it easier~
def myfunc(x, df2):
    return df2["set1"] + df2["set2"] * x

df1 = df1.join(df1.x.apply(myfunc, args=(df2,)).add_prefix('run'))
print(df1)

# Output:

   x  run0  run1  run2  run3
0  1   100   200   300   400
1  2   200   400   600   800
2  3   300   600   900  1200

That said, there's often a way to do whatever you want to do using pandas methods:也就是说,通常有一种方法可以使用 pandas 方法做任何你想做的事情:

df = df1.merge(df2, 'cross')
df['value'] = df.set1 + df.set2 * df.x
df['run'] = df.groupby('x')['value'].cumcount() + 1
df = df.pivot(index='x', columns='run', values='value')
df.columns = [f'{df.columns.name}{x}' for x in df.columns]
print(df.reset_index())

# Output:

   x  run1  run2  run3  run4
0  1   100   200   300   400
1  2   200   400   600   800
2  3   300   600   900  1200

You can do你可以做

df1 = df1.join(df1.apply(lambda x  : myfunc(df2, x['x']),axis=1))
Out[152]: 
   x    0    1    2     3
0  1  100  200  300   400
1  2  200  400  600   800
2  3  300  600  900  1200

This is specific to your example myfunc but it is possible to vectorize with dot这特定于您的示例myfunc但可以使用dot进行矢量化

df1[['x']].dot(
    df2['set1'].add(df2['set2']).to_frame().T.values
).rename(
    columns={i:f'run{i+1}' for i in df2.index}
).assign(
    x = df1['x'],
)

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

相关问题 将自定义函数应用于pandas数据框中的每一行的更快方法? - Faster way to apply custom function to each row in pandas dataframe? 按组将函数应用于 Pandas 数据框中的每一行 - Apply function to each row in Pandas dataframe by group 用于将函数应用于 Pandas DataFrame 中的每一行的应用函数的替代方法 - Alternative to apply function for applying a function to each row in Pandas DataFrame 如何将函数应用于数据帧的每一行并返回结果 - How to apply a function to each row of a dataframe and get the results back 将函数应用于pandas数据帧的每一行以创建两个新列 - Apply function to each row of pandas dataframe to create two new columns 在没有 for 循环的情况下,将包含 if 的函数应用于 pandas 中数据帧的每一行 - Apply a function including if to each row of a dataframe in pandas without for loop 如何将 function 应用于 pandas dataframe 中一列的每一行? - How to apply a function to each row of one column in a pandas dataframe? 将函数应用于pandas数据框列中每一行的每个单词 - apply function to each word of every row in pandas dataframe column 如何将 function 应用于 pandas dataframe 中的每一行? - How can I apply a function to each row in a pandas dataframe? 将自定义函数应用于pandas数据框中的每个列组合 - Apply custom function to each combination of columns in a pandas dataframe
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM