'Python Adding Index to the list as a column and converting it to the data frame

I am using lime package to explain my output and would like to get the index as a column for all the records in the predictive dataframe. I have used zip to combine the index to the list.

x=list(X_test.index)
l1=
output=
for i in range(np.array(X_test.shape[0])):
exp_data = explainer.explain_instance(np.array(X_test)[i], random_forest.predict_proba,
num_features=10)
a=exp_data.as_list()
l1.append(a)

for elem1,elem2 in zip(x,l1):
o1=list((elem1,elem2))
output=output + o1
print(output)
[6334, [((x10,x11,x12…)],12743, [(y110…]]

This is giving me two lists. Then when i converted it to dataframe, It is giving the index in separate row instead of same row.

df1 = pd.DataFrame([output]).T
df1.columns=[‘a1’]

                                             a1
0 6334
1 [(x10,x11,x12…]
2 12743
3 [(y110…]

Ideally, I would like to have recorded/index with the list of values in the same row as a comma-separated column like other features for that record. for eg

a1

0 [(6334,x10,x11,x12)]
1 [912743,y110,…)]

Thanks for your help.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source