小编M_I*_*845的帖子

熊猫:应用带有列和变量作为参数的函数

我正在尝试将具有多个参数的函数应用于数据框,其中两个参数需要分配给数据框的行,而一个是变量(简单数字)。

对于行,类似线程的变体适用:(与我的原始函数相比,所有函数都被简化了)

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)

谢谢你的帮助!

python apply dataframe python-3.x pandas

4
推荐指数
1
解决办法
1995
查看次数

计算 Pandas DataFrame 中父级总数的份额

我想知道与下面的方法相比是否有更好的方法来计算 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)

python dataframe pandas pandas-groupby

3
推荐指数
1
解决办法
1556
查看次数

标签 统计

dataframe ×2

pandas ×2

python ×2

apply ×1

pandas-groupby ×1

python-3.x ×1