在第10和11行中,如何将这两行放入函数中,并仍然在其中返回带有两个名称的df列?本质上,我们如何将第10行和第11行放入函数中?
import pandas as pd
w = pd.Series(['BAIN', 'BAIN', 'BAIN', 'KPMG', 'KPMG', 'KPMG', 'EY', 'EY', 'EY' ])
x = pd.Series([2020,2019,2018,2020,2019,2018,2020,2019,2018])
y = pd.Series([10000, 10000, 20000, 25000, 50000, 10000, 100000, 50500, 120000])
z = pd.Series([100000, 500000, 1000000, 50000, 100000, 40000, 1000, 500, 4000])
df = pd.DataFrame({'consultant': w, 'fiscal_year':x, 'budgeted_cost':y, 'actual_cost':z})
indexer_consultant_fy = ['consultant', 'fiscal_year']
df = df.set_index(indexer_consultant_fy).sort_index(ascending=True)
df['budgeted_percent_change_by_year'] = df.groupby(level=['consultant'])['budgeted_cost'].pct_change(fill_method='ffill') #put into a function?
df['actual_percent_change_by_year'] = df.groupby(level=['consultant'])['actual_cost'].pct_change(fill_method='ffill') #put into a function?
df = df.sort_values(by = ['consultant', 'fiscal_year'], ascending=False)
df['actual_budget_pct_diff'] = df.pct_change(axis='columns',fill_method='ffill')['actual_cost']