简体   繁体   English

应用函数和返回多列的最pythonic方法是什么?

[英]What is the most pythonic way to apply a function on and return multiple columns?

While using Pandas, I often encounter a case where there is an existing function which takes in multiple arguments and returns multiple values: 在使用Pandas时,我经常会遇到一个现有函数,它接受多个参数并返回多个值:

def foo(val_a, val_b):
    """
    Some example function that takes in and returns multiple values.
    Can be a lot more complex.
    """
    sm = val_a + val_b
    sb = val_a - val_b
    mt = val_a * val_b
    dv = val_a / val_b
    return sm, sb, mt, dv

Suppose I have a dataframe: 假设我有一个数据帧:

import pandas as pd
df = pd.DataFrame([[1, 2], [3, 4], [5, 6], [7, 8]])
df
Out[6]: 
   0  1
0  1  2
1  3  4
2  5  6
3  7  8

What I want is to apply foo on df with column 0 and 1 as arguments, and put the results into new columns of df , without modifying foo , like this: 我想要的是在df上将foo应用于第0列和第1列作为参数,并将结果放入df新列中, 而不修改foo ,如下所示:

df_out
Out[7]:
   0  1  su  sb  mt  dv
0  1  2  3   -1  2   0.5
1  3  4  7   -1  12  0.75
2  5  6  11  -1  30  0.833
3  7  8  15  -1  56  0.875

What is the most pythonic way to achieve this? 实现这一目标的最pythonic方法是什么?

>>> pd.concat([df, df.from_records(foo(df[0], df[1])).T], axis=1)
   0  1     0    1     2         3
0  1  2   3.0 -1.0   2.0  0.500000
1  3  4   7.0 -1.0  12.0  0.750000
2  5  6  11.0 -1.0  30.0  0.833333
3  7  8  15.0 -1.0  56.0  0.875000

Speed: 1.13 ms per loop 速度:每循环1.13 ms

If you care about speed this is superior to using apply and gives your desired output. 如果您关心速度,这优于使用apply并提供您想要的输出。

>>> pd.concat([df, df.from_records(np.vectorize(foo)(df[0], df[1])).T], axis=1)

Speed: 728 µs per loop 速度:每回路728μs

#apply function foo and generate a DF using return values and then merge into existing DF.
merged = pd.merge(df,df.apply(lambda x: pd.Series(foo(x[0],x[1])),axis=1),left_index=True,right_index=True)
#change column names.
merged.columns=[0,1,'sm','sb','mt','dv']

merged
Out[1478]: 
   0  1    sm   sb    mt        dv
0  1  2   3.0 -1.0   2.0  0.500000
1  3  4   7.0 -1.0  12.0  0.750000
2  5  6  11.0 -1.0  30.0  0.833333
3  7  8  15.0 -1.0  56.0  0.875000

You can use apply + DataFrame constructor: 您可以使用apply + DataFrame构造函数:

cols = ['sm','sb','mt','dv']
df[cols] = pd.DataFrame(df.apply(lambda x: foo(x[0], x[1]), 1).values.tolist(),columns= cols)
print (df)
   0  1  sm  sb  mt        dv
0  1  2   3  -1   2  0.500000
1  3  4   7  -1  12  0.750000
2  5  6  11  -1  30  0.833333
3  7  8  15  -1  56  0.875000

Solution with concat 解决方案与concat

cols = ['sm','sb','mt','dv']
df[cols] = pd.concat(foo(df[0], df[1]), axis=1, keys=cols)
print (df)
   0  1  sm  sb  mt        dv
0  1  2   3  -1   2  0.500000
1  3  4   7  -1  12  0.750000
2  5  6  11  -1  30  0.833333
3  7  8  15  -1  56  0.875000

Also is possible create new DataFrame and then concat original: 也可以创建新的DataFrame然后concat原始:

cols = ['sm','sb','mt','dv']
df1 = pd.concat(foo(df[0], df[1]), axis=1, keys=cols)
print (df1)
   sm  sb  mt        dv
0   3  -1   2  0.500000
1   7  -1  12  0.750000
2  11  -1  30  0.833333
3  15  -1  56  0.875000

df = pd.concat([df, df1], axis=1)
print (df)
   0  1  sm  sb  mt        dv
0  1  2   3  -1   2  0.500000
1  3  4   7  -1  12  0.750000
2  5  6  11  -1  30  0.833333
3  7  8  15  -1  56  0.875000

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

相关问题 什么是有条件地返回函数的最pythonic方式 - What is the most pythonic way to conditionally return a function 在具有多种类型的空白字符的字符串中的每个单词上应用函数的最有效的方法是什么? - What's the most pythonic way to apply a function on every word in a string with multiple types of white space characters? 将 function 应用于列表的某些项目的最pythonic方法是什么? - What is the most pythonic way to apply function to some items of a list? 大多数pythonic的功能方式没有回报? - Most pythonic way of function with no return? 在多次调用同一函数时重用数据的最pythonic方法是什么? - What is the most pythonic way to reuse data in multiple calls to same function? 修改函数函数的最Pythonic方法是什么? - What is the most Pythonic way to modify the function of a function? 在此函数中传递kwargs的最pythonic方法是什么? - What is the most pythonic way to pass kwargs in this function? 处理多个条件函数的大多数pythonic方式 - Most pythonic way of approaching a multiple conditionals function 将函数多次应用于对象的pythonic方法 - pythonic way to apply function to object multiple times 组合多个字符串列以创建新的 Pandas 系列的最pythonic 方法是什么? - What is the most pythonic way to combine multiple string columns to create a new Pandas series?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM