I have a following problem. I am creating a heat map. To make my dataframe more "heatmap friendly" I need to fill missing values on X and Y axis. This is how my dataframe looks like now:
I need to go through both axis (range 20 - 393 for axis = 0 and 584 - 648 for axis = 1) and if the axis is missing in my dataframe thwn insert a column full of zeros.
I followed the advice here (https://stackabuse.com/ultimate-guide-to-heatmaps-in-seaborn-with-python/) and I tried:
df = df.reindex(range(20,393), axis=0, fill_value=0)
df = df.reindex(range(584,648), axis=1, fill_value=0).astype(int)
but it rewrites all existing values to zero (including the nonzero ones). Can you help me, please? This answer seems not relevant for my case: Insert missing rows in a dataframe (variable index range)
EDIT: Reproducible example:
import pandas as pd
my_df = pd.DataFrame(data={'Centroid1': [20, 20, 22, 26, 27],
'Centroid2': [584, 596, 597, 608, 602],
'counts': [2, 0, 0, 1, 1]})
my_df = my_df.reset_index().pivot(index='Centroid2',
columns='Centroid1', values='counts')
my_df.fillna(0, inplace=True)
#HOW TO FILL MISSING COLUMNS 20-30 and 582-605???
#examples bellow does not work - everything is zero
#Ex 1:
my_df = my_df.rename(columns=int, index=int)
my_df = my_df.reindex(index=range(20,30), columns=range(582,605), fill_value=0)
#Ex 2:
my_df = my_df.reindex(range(20,30), axis=0, fill_value=0)
my_df = my_df.reindex(range(582,605), axis=1, fill_value=0).astype(int)