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
  • 37,170
  • 13
  • 92
  • 132
Gavin
  • 1,131
  • 5
  • 13
  • 28

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
  • 9,832
  • 8
  • 52
  • 96