Skip to content
Advertisement

Problems with Multiindex from pd.Crosstab when appending non present row data

Morning All,

Happy Friday. I have some excel output which shows Client, their Sector and the counts of their Result via a crosstab command. This works well:

JavaScript

The excel extract meets the requirements.

enter image description here

An additional request has come in to add all other possible clients which are not present in the current months data, but may be in futures months. For each Client with no data add a row in the crosstab, and for each field insert; #NA. The final output would be:

enter image description here

I looked to add theses rows via:

JavaScript

The issue is that results of the crosstab are multiindexed. I tried flattening the crosstab output with df = df.stack([0]).reset_index() but this changed the structure completely and deviates from the final output entirely. I now get TypeError: Expected tuple, got str

Any help would be appreciated.

Advertisement

Answer

You can try reindex

JavaScript
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement