'iterate over numpy arrays to fill pandas dataframe
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?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|

