Home > Mobile >  Inserting a dataframe row into another dataframe using the name of the index value
Inserting a dataframe row into another dataframe using the name of the index value

Time:08-02

Basically the middle image shows the current values after all iterations are complete. I tried many ways to try to update the index "0" to be the particular ConfigurationLevel_ value e.g. CongigurationLevel_1 but I have had no success. While I'm able to create a dataframe with the correct index values, I'm not able to get the actual values in them. I'm guessing that a solution would solve both ways, that I need to find out how to get:

Combined_SHAP_df[ConfigurationLevel_i] = SHAP_Level_df[0]

(I have SHAP_Level_df[0] as each dataframe created only has a single row).

ConfigurationList = []
    for i in range(OutputDim):
        ConfigurationList.append("ConfigurationLevel_"   str(i   1))

Combined_SHAP_df = pd.DataFrame(index=ConfigurationList)
Combined_SHAP_df = pd.concat([Combined_SHAP_df, SHAP_Level_df])

where SHAP_Level_df is a single row in a dataframe (this is in a loop so these are continually produced and concatenated.

SHAP_Level_df contents

This is the issue:

Assigning row values to an named index

Whereas the final product would look like this:

enter image description here

CodePudding user response:

You can try:

pd.concat([df1.reset_index(drop=True), df2.reset_index(drop=True)], axis=0, ignore_index=True)

CodePudding user response:

Instead of creating a dataframe with the index names initially create the basic dataframe without indexes:

Combined_SHAP_df = pd.DataFrame()

and after the dataframe has been completely filled with values, ie. after the loop containing:

Combined_SHAP_df = pd.concat([Combined_SHAP_df, SHAP_Level_df])

as the number of rows of Combined_SHAP_df will be equal to the number of index names we can set the .index names to the list of names:

Combined_SHAP_df.index = ConfigurationList

This just replaces all of the index values with the new values.

  • Related