我正在尝试将具有多个参数的函数应用于数据框,其中两个参数需要分配给数据框的行,而一个是变量(简单数字)。
对于行,类似线程的变体适用:(与我的原始函数相比,所有函数都被简化了)
import pandas as pd
dict={'a':[-2,5,4,-6], 'b':[4,4,5,-8]}
df=pd.DataFrame (dict)
print(df)
def DummyFunction (row):
return row['a']*row['b']
#this works:
df['Dummy1']=df.apply(DummyFunction, axis=1)
Run Code Online (Sandbox Code Playgroud)
但是,如何在函数接受附加参数(固定变量)的情况下应用以下变体?我似乎找不到办法将它传递给apply方法:
def DummyFunction2(row, threshold):
return row['a']*row['b']*threshold
# where threshold will be assigned to a number?
# I don't seem to find a viable option to fill the row argument below:
# df['Dummy2']=df.apply(DummyFunction2(row,1000), axis=1)
Run Code Online (Sandbox Code Playgroud)
谢谢你的帮助!
我想知道与下面的方法相比是否有更好的方法来计算 Pandas 中父级总数的份额:非常感谢您的帮助!
raw_data = {'product': ['A', 'A', 'A', 'B', 'B', 'B', 'C', 'C', 'C'],
'revenue': [10,20,20,0,50,50,0,0,30]}
df = pd.DataFrame(raw_data, columns = ['product', 'revenue'])
unique_values = df['product'].unique()
L = pd.DataFrame ()
for value in unique_values:
small_df = df[df['product']==value]
small_df['shares'] = small_df['revenue']/small_df['revenue'].sum()
L = L.append(small_df, ignore_index=True)
print(L)
Run Code Online (Sandbox Code Playgroud)