2

I have the following nested_dict:

{'view_0': {'spain': -1}, 'view_1': {'portugal': 0}, 'view_2': {'morocco': 1.0, 'france': -1.0}, 'view_3': {'germany': 0.5, 'italy': 0.5, 'uk': -0.5, 'ireland': -0.5}}

On the other side, I have the following empty_df, wherein the index appears the keys of the nested_dict. and in the columns the key found in the values of each nested_dict.

            spain  portugal  morocco  france  germany  italy  uk   ireland
view_0          0    0         0        0       0       0      0      0             
view_1          0    0         0        0       0       0      0      0       
view_2          0    0         0        0       0       0      0      0       
view_3          0    0         0        0       0       0      0      0       

I would like to place the values.values() of nested_dict in the empty_df to obtain the following output:

            spain  portugal  morocco  france  germany  italy  uk   ireland
view_0         -1    0         0        0       0       0      0      0             
view_1          0    0         0        0       0       0      0      0       
view_2          0    0         1       -1       0       0      0      0       
view_3          0    0         0        0      0.5     0.5   -0.5   -0.5

And in order to do so I tried a

empty_df.replace(nested_dict)

However returns the empty_dict filled with zeros, not substituting the values.

2 Answers 2

1

If possible use DataFrame.from_dict and replace empty values by fillna:

df = pd.DataFrame.from_dict(d, orient='index').fillna(0)

Also is possible add reindex for same columns and index names in same ordering like empty_df:

df = (pd.DataFrame.from_dict(d, orient='index')
                  .reindex(columns=empty_df.columns, index=df_empty.index)
                  .fillna(0))

print (df)
        spain  portugal  morocco  france  germany  italy   uk  ireland
view_0   -1.0       0.0      0.0     0.0      0.0    0.0  0.0      0.0
view_1    0.0       0.0      0.0     0.0      0.0    0.0  0.0      0.0
view_2    0.0       0.0      1.0    -1.0      0.0    0.0  0.0      0.0
view_3    0.0       0.0      0.0     0.0      0.5    0.5 -0.5     -0.5
Sign up to request clarification or add additional context in comments.

Comments

1

Construct a dataframe from your dictionary and use pd.DataFrame.update:

df_data = pd.DataFrame.from_dict(d, orient='index')

df.update(df_data)

print(df)

        spain  portugal  morocco  france  germany  italy   uk  ireland
view_0   -1.0       0.0      0.0     0.0      0.0    0.0  0.0      0.0
view_1    0.0       0.0      0.0     0.0      0.0    0.0  0.0      0.0
view_2    0.0       0.0      1.0    -1.0      0.0    0.0  0.0      0.0
view_3    0.0       0.0      0.0     0.0      0.5    0.5 -0.5     -0.5

Comments

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.