This code creates a practice dataframe for instruction in a format that is familiar with finance analysts. Analysts typically deal with profit and loss statement data in excel spreadsheets, often pulled or aggregated from an ERP source system and this example shows a toy example that has some relevant fields and formatting.
import pandas as pd
# For convenience while using a Jupyter Notebook,
# display dataframe floats as currency & round to two decimal places
= '${:,.2f}'.format
pd.options.display.float_format
def make_pnl():
= pd.DataFrame(data={'ProductLine': ['A', 'A', 'B', 'B'],
df 'FunctionalArea': ['REV', 'COS']*2,
'AccountL1': ['Revenue', 'Cost of Sales']*2,
'201901 ACT': [200., 100., 150., 75.],
'202001 ACT': [210., 105., 150., 75.],
'202001 POR': [205., 105., 150., 75.]
})return df
= make_pnl() df
This is a very simplified version of the data that would be familiar to analysts. In reality, there would be many more fields and values, depending on the level of granularity that’s available. The columns like ‘201901 ACT’ are time periods in a YYYYQQ format and the last three letters signify whether it is an actual or forecast (POR or Plan of Record) value.
Multi-Index DataFrame Slicing
It might also be useful to make this a multiindex dataframe for easier grouping/slicing and to reduce the amount of space the dataframe takes up in memory.
'ProductLine', 'FunctionalArea', 'AccountL1'], inplace=True) df.set_index([
You may need to use sets to slice among items in your index. Use slice(None)
to skip levels in your index if needed.
'A', slice(None), 'Cost of Sales')] df.loc[(
and returns:
Which is the same as using the query()
method:
"ProductLine=='A' & AccountL1=='Cost of Sales'") df.query(
Converting from Wide to Long format with pd.melt()
Another useful transformation is to ‘unpivot’ the data with pd.melt()
.
= ['ProductLine', 'FunctionalArea', 'AccountL1']
id_vars = df.reset_index().melt(id_vars=id_vars, var_name='Period', value_name='Value') dfl
Join Previous Cycle for Recons
You’ll often need to reconcile PnL data against other time periods, like prior quarters, prior years, actuals vs forecasts, and so on.
In a spreadsheet you’d manually manipulate and select columns and subsets of data to perform this. In Python, you can perform a SQL-style lookup and join so that you create those reconciliations in a table of data that you can slice and lookup later.
= ['ProductLine', 'FunctionalArea', 'AccountL1']
id_vars
# Pull in data in long format
= df.reset_index().melt(id_vars=id_vars, var_name='Period', value_name='Value')
dfl
# Create new features by parsing text
'YYYY'] = dfl['Period'].apply(lambda x: x[:4]).astype(int)
dfl['Q'] = dfl['Period'].apply(lambda x: x[5]).astype(int)
dfl[
# Use new features to calculate lookup columns
'Prior_Year_Period'] = ((dfl['YYYY'] - 1) *100 + dfl['Q']).astype(str) + " ACT"
dfl[
# Create a subset to use as the right dataframe
= dfl[id_vars + ['Period', 'Value']].rename({'Value': 'PY_Value'}, axis=1)
df2
# Left Join the original dataframe with the right dataframe
= dfl.merge(df2,
dfNew =id_vars+['Prior_Year_Period'],
left_on=id_vars+['Period'],
right_on='left',
how=["", "_DROP"])
suffixes
# Drop duplicate columns
filter(regex='_DROP$').columns.tolist(),axis=1, inplace=True) dfNew.drop(dfNew.
And we see the result works. In this example, we don’t have 201801 ACT data so it’s a np.nan
value, but you see the rest of the values filled in as expected for prior year values.