Turning a table column into a list in Python

Asked

Viewed 1,643 times

1

importances_DT = pd.DataFrame({'feature':x.columns,'importance':np.round(decision_tree.feature_importances_,3)})
importances_DT = importances_DT.sort_values('importance',ascending=False).set_index('feature')

How can I just "take" the Feature column and put it on a list?

I tried the following line of code:

lista = importances_DT ["feature"].tolist()

1 answer

2

Patricia is just like you tried to do.
See the example below, and if you want to test it you can access here.

import pandas as pd

dados = {'feature': [1, 2], 'importance': [3, 4]}

df = pd.DataFrame(dados)
df.sort_values('importance',ascending=False).set_index('feature')

lista = df['feature'].tolist()

print(lista)
for item in lista:
  print(item)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.