pythonpandasdataframeseriesoutput-formatting

Remove name, dtype from pandas output of dataframe or series


I have output file like this from a pandas function.

Series([], name: column, dtype: object)
311     race
317     gender
Name: column, dtype: object

I'm trying to get an output with just the second column, i.e.,

race
gender

by deleting top and bottom rows, first column. How do I do that?


Solution

  • You want just the .values attribute:

    In [159]:
    
    s = pd.Series(['race','gender'],index=[311,317])
    s
    Out[159]:
    311      race
    317    gender
    dtype: object
    In [162]:
    
    s.values
    Out[162]:
    array(['race', 'gender'], dtype=object)
    

    You can convert to a list or access each value:

    In [163]:
    
    list(s)
    Out[163]:
    ['race', 'gender']
    
    In [164]:
    
    for val in s:
        print(val)
    race
    gender