我是熊猫的新手,请原谅。
我已经可以使用以下代码实现所需的输出:-
import pandas as pd
pd.options.display.float_format = '£{:,.2f}'.format
data=pd.read_csv('calls.csv')
def markupfunc(df):
if ( "ppm" not in df['Description']):
return df['Salesprice'] * markup
else:
return df['Salesprice']
def seconds_to_minutes(df):
return int(df['Duration'] / 60)
data['Customer Price'] = data.apply(markupfunc, axis = 1)
data['Total Call Minutes'] = data.apply(seconds_to_minutes, axis = 1)
data['Call Count'] = 1
data['Network Access Reference'] = data['Network Access Reference'].str.replace('\d+', '')
data.rename(columns={'Salesprice':'Gamma Cost','Duration':'Duration (s)'}, inplace=True)
data.groupby(['Network Access Reference']).agg({'Gamma Cost':sum,'Customer Price':sum,'Call Count':'count','Total Call Minutes':sum})
虽然效果很好,但我很好奇如何通过在最终data.groupby
语句中使用lambda语句而不是使用函数来改进代码。我尝试了多种方法,但是每次收到错误消息时,都会尝试。
真正的感谢和指导。