0

I'm trying to create a heatmap with some data. For this I've created an empty bidimensional dataframe

dft = pd.DataFrame(0, index=matrix_values_index , columns= matrix_values_columns)

Which gives me a 58x58 matrix.

Bidimensional dataframe

On the other hand, there are some data within an excel spreadsheet which I've transformed into a multiindex dataframe

df2 = heatmap_values.groupby(['ID Requester','ID Supplier']).agg({'ID Supplier':'count'})

df2.rename(columns={'ID Supplier': 'No. Interdependencies'})

The first index is the index of the bidimensional dataframe

The second index is the column of the bidimensional dataframe

This is the multiindex data frame

Is there a way to update the bidimensional dataframe with the values of the multiindex dataframe without looping over it?

I've been looking for a solution for it all over the internet, but without success so far.

Do you have any suggestions for this problem?

Thanks in advance.

1 Answer 1

1

Use crosstab with DataFrame.reindex if some index or columns names missing instead your solution:

dft = (pd.crosstab(heatmap_values['ID Requester'],heatmap_values['ID Supplier'])
         .reindex(index=matrix_values_index, columns= matrix_values_columns, fill_value=0))

Your solution should be changed for same ouput with Series.unstack and also reindex:

df2 = heatmap_values.groupby(['ID Requester','ID Supplier']).agg({'ID Supplier':'count'})

df2 = df2.rename(columns={'ID Supplier': 'No. Interdependencies'})

dft = (df2['No. Interdependencies'].unstack(fill_value=0)
                                   .reindex(index=matrix_values_index,
                                            columns= matrix_values_columns, 
                                            fill_value=0))
Sign up to request clarification or add additional context in comments.

4 Comments

I'm trying your approach but not it's not working. I've replaced the (df['ID Requester'],df['ID Supplier']) by (df2.index.names[0],df2.index.names[1]) since it is a Multiindex dataframe, but without success so far
@Nikolas - Added your solution to my answer.
Hello @jezrael I've used some of your tips and finally succeded: dfn = df2.unstack() dfn_reduced = dfn.xs('ID IPM Supplier', axis=1, drop_level=True).fillna(0) dfn_full = dfn_reduced.reindex(index=matrix_values_index , columns= matrix_values_columns, fill_value=0) –
@Nikolas - So data are different like df2 = heatmap_values.groupby(['ID Requester','ID Supplier']).agg({'ID Supplier':'count'}) df2 = df2.rename(columns={'ID Supplier': 'No. Interdependencies'}), so not possible use df2['No. Interdependencies'].unstack(fill_value=0) instead dfn = df2.unstack() dfn_reduced = dfn.xs('ID IPM Supplier', axis=1, drop_level=True).fillna(0) ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.