I have a dataframa as below:
Symbol Change Yrows Xcols 0 ADANIPORTS 7.15 1 1 1 BAJAJ-AUTO 3.04 1 2 2 HINDUNILVR 3.00 1 3 3 M&M -2.69 2 1 4 UPL -2.92 2 2 5 NTPC -3.15 2 3 6 JSWSTEEL -3.30 3 1 7 COALINDIA -3.38 3 2 8 ONGC -3.48 3 3 Here my Dataframe consists of 9 rows hence below code runs fine. But when dataframe size reduces to 8 it gives reshape error.
I need help on below 2 conditions.
- I want to plot the heat map without that 9th row when not available.
- How to automatically reshape of HeatMap table with available data of dataframe.
My Code as below:
df = pd.read_csv("Heatmap_data.csv") Yrows = [1,1,1,2,2,2,3,3,3] Xcols = [1,2,3,1,2,3,1,2,3] df = df.assign(Yrows=Yrows, Xcols=Xcols) symbol = ((np.asarray(df['Symbol'])).reshape(3, 3)) perchange = ((np.asarray(df['Change'])).reshape(3, 3)) result = df.pivot(index='Yrows', columns='Xcols', values='Change') labels = (np.asarray(["{0} \n {1:.2f}%".format(symb, value) for symb, value in zip(symbol.flatten(), perchange.flatten())])).reshape(3, 3) fig, ax = plt.subplots(figsize=(13, 7)) title = "Heat Map" plt.title(title, fontsize=18) ttl = ax.title ttl.set_position([0.5, 1.05]) ax.set_xticks([]) ax.set_yticks([]) ax.axis('off') sns.heatmap(result, annot=labels, fmt="", cmap='RdYlGn', linewidths=0.40, ax=ax) plt.show() I want my desired out put with dataframe of 8 rows as below:

