在使用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
假设我有一个数据帧:
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
我想要的是在foo
上将{0}和第0列作为参数应用df
,并将结果放入df
的新列,而不修改{{1像这样::
foo
实现这一目标的最佳方式是什么?
答案 0 :(得分:1)
>>> 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
速度:每循环1.13 ms
如果您关心速度,这优于使用apply
并提供您想要的输出。
>>> pd.concat([df, df.from_records(np.vectorize(foo)(df[0], df[1])).T], axis=1)
速度:每循环728μs
答案 1 :(得分:1)
#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
答案 2 :(得分:1)
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
的解决方案
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
也可以创建新的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