Search code examples
pythonpandasdata-analysis

How to extract a list from pandas?


I'd like to extract and return just the contents, values, of an object in pandas.

I have for example...

>>> xx[xx['Business']=="1"]['Label']
3     Sloan School of Management
22                      Business
46       Business Administration

I'd like just the list of labels, no index, no panda object, no array. How can I extract only the data to return a list of the form...

["Sloan School of Management", "Business", "Business Administration"]

Trying xx[xx['Business']=="1"]['Label'].values produced an array:

array([Sloan School of Management, Business, Business Administration,
       Professional Development, Business & Management], dtype=object)

There must be a built-in and fairly common command for what I'm trying to do, right?


Solution

  • xx[xx['Business']=="1"]['Label'].tolist()
    

    On a related note, I've found IPython's tab completion to be invaluable in my workflow as it helps list all functions of a class. Personally, it was worth the effort to install/configure