I have data in excel extracted from IBM Cube in the form of Cross tab.
Crosstab example:
|Account| Entity| Functions| JAN | FEB | MAR | JAN | Feb | Mar |
Actuals Actuals Actuals Forecast Forecast Forecast
A2100 10021 ABS $200 $300 $270 $230 $270 $250
A2200 20023 GBS $320 $285 $360 $350 $300 $400
How to read cross tab in the panda data frame and convert it into the columnar format? eventually, I want to create functions which can show the differences like (actuals minus forecast) on selection of Month and Functions.
Disclaimer:- I am new to python, any directions will be helpful. I am trying to understand if there is any way to achieve this? I only know simple excel read and csv read which requires data to be in columnar form.
df = pd.read_excel("<path to your file>.xlsx")
final output should look like as suggested by Stef, in addition there should be a column showing variance (Forecast-Actual)
Assuming that you already read your data from Excel into a dataframe df
you can use melt
and merge
to unpivot your data like this:
import pandas as pd
data = {'Account': [None, 'A2100', 'A2200'],
'Entity': [None, '10021', '20023'],
'Functions': [None, 'ABS', 'GBS'],
'JAN': ['Actuals', '$200', '$320'],
'FEB': ['Actuals', '$300', '$285'],
'MAR': ['Actuals', '$270', '$360'],
'JAN.1': ['Forecast', '$230', '$350'],
'Feb': ['Forecast', '$270', '$300'],
'Mar': ['Forecast', '$250', '$400']}
df = pd.DataFrame(data)
ma = df.iloc[0].ne('Forecast') # mask for actuals
dfa = df.loc[1:,ma.index[ma]]
mf = df.iloc[0].ne('Actuals') # mask for forcasts
dff = df.loc[1:,mf.index[mf]]
dff.columns = dfa.columns
res = pd.melt(dfa, ['Account','Entity','Functions'],
var_name='Month',
value_name='Actuals').merge(
pd.melt(dff, ['Account','Entity','Functions'],
var_name='Month',
value_name='Forecast'))
Result:
Account Entity Functions Month Actuals Forecast
0 A2100 10021 ABS JAN $200 $230
1 A2200 20023 GBS JAN $320 $350
2 A2100 10021 ABS FEB $300 $270
3 A2200 20023 GBS FEB $285 $300
4 A2100 10021 ABS MAR $270 $250
5 A2200 20023 GBS MAR $360 $400
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.