0

I have select a string column from a table. Now I want to take all the values and convert to a list of string. I used the following code but it does not work.

top_feature = rf_varimp[['variable']].values.tolist()
top_feature

the output is:

[['mrkd100006'], ['mrkd100005'], ['ccs2376'], ['ccs644'], ['ccs3262']]

what I expect for the output is something like:

['mrkd100006', 'mrkd100005', 'ccs2376', 'ccs644', 'ccs3262']
pault
  • 41,343
  • 15
  • 107
  • 149
Gavin
  • 1,411
  • 5
  • 18
  • 31

2 Answers2

1

Pandas series can be cast to lists directly

list(rf_varimp['variable'])

Out:
['mrkd100006', 'mrkd100005', 'ccs2376', 'ccs644', 'ccs3262']
Hli
  • 11
  • 3
0

You are returning a multi dimensional array by using

rf_varimp[['variable']].values.tolist()

Use the following instead

rf_varimp['variable'].values.tolist()

There is a subtle difference between rf_varimp[['variable']] and rf_varimp['variable'] in that one is a pandas.DataFrame and the latter is a pandas.Series. Knowing the difference will save you a lot of hassle in the future with other pandas operations.

Alexander McFarlane
  • 10,643
  • 9
  • 59
  • 100