Search code examples
pythonpandasseriespandas-loctype-coercion

how to force pandas .loc to return series


I have a Pandas DataFrame with NAME index and one of the columns called CLASS:

df = 
        CLASS
NAME    
John    Math
John    Science
Lisa    Music

Now if I extract df.loc['John', 'CLASS'], it returns a Series, but if I extract df.loc['Lisa', 'CLASS'] it returns a str.

Is there a way for me to force the returned value to be Series (i.e. ['Music', ])? I need to iterate through that returned object right afterwards and I don't want to go through Math, Science in John's case (desired) but M, u, s, i, c in Lisa's case (not desired).


Solution

  • To force loc to return a Series, wrap your index value(s) inside a list literal.

    df.loc[['John'], 'CLASS']
    
    NAME
    John       Math
    John    Science
    Name: CLASS, dtype: object
    

    df.loc[['Lisa'], 'CLASS']
    
    NAME
    Lisa    Music
    Name: CLASS, dtype: object
    

    This causes a Series with 1 (or more) rows to be returned regardless. Also works when the list is empty, returning an empty list:

    df.loc[[], 'CLASS']
    Series([], Name: CLASS, dtype: object)