1

I have two numpy arrays and dataframe as given below

val = np.array([0.501,0.32])
values = np.arange(24).reshape((2,3,4))
input_df = pd.DataFrame(columns=['colname_' + str(i) for i in range(4)])
row_id_list = [20,40]

I would like to

a) Create a new dataframe (dummy) with 3 columns such as ROW_ID, FEATURE NAME, Contribution

b) values for dummy dataframe should be populated using np.array above and column names from input_df`

c) Under the Feature Name column use the input_df column names

b) Populate the val[0] as contribution in dummy dataframe and also use each element from values[0][1] to populate it in contribution column.

I tried the below code with the help of this post

   pd.DataFrame({
  "Feature Name": ["Base value"] + [f"{col}" for col in df.columns.tolist()],
  "Contribution": (val[:1].tolist()) + list(values[0][1])
  #                   ^^^^
})

I was trying something like below

for i in range(len(input_df )):
    print("explainer value is")
    print(val[:1].tolist())
    print("the values are")
    print(values[0][i].tolist())

While this does for a single row, I would like to do this for 2000 rows. Is there any efficient way to do this?

enter image description here

2
  • for each row, we have to fill rows, base_value and number of columns available in input dataframe Commented Mar 31, 2022 at 17:26
  • How is the ROW_ID associated with your data? Each ROW_ID is associated with a value in val? How about values? Commented Mar 31, 2022 at 17:35

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.