53

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?

smci
  • 32,567
  • 20
  • 113
  • 146
pam
  • 1,175
  • 5
  • 15
  • 28

3 Answers3

47

DataFrame/Series.to_string

These methods have a variety of arguments that allow you configure what, and how, information is displayed when you print. By default Series.to_string has name=False and dtype=False, so we additionally specify index=False:

s = pd.Series(['race', 'gender'], index=[311, 317])

print(s.to_string(index=False))
#   race
# gender

If the Index is important the default is index=True:

print(s.to_string())
#311      race
#317    gender

Series.str.cat

When you don't care about the index and just want the values left justified cat with a '\n'. Values need to be strings, so convert first if necessary.

#s = s.astype(str)

print(s.str.cat(sep='\n'))
#race
#gender
ALollz
  • 57,915
  • 7
  • 66
  • 89
  • 1
    I like how this is explicit, but it left whitespace at the beginning of my string. In my case I can remove it with `strip()` but if there is a possibility that your dataframe element actually starts with whitespace, that could be dangerous. – craq Jul 03 '20 at 03:21
  • 1
    @craq Good point. The default left-justification of the index and right-justification of values makes it very difficult to get the proper justification with the `to_string` methods if you don't need the index Instead `Series.str.cat` is the best solution as it will preserve initial white-space, if any – ALollz Jul 03 '20 at 04:45
  • I suggest you add that `to_string()` allows much more fine-grained control of name, index, header, also length, dtype, float_format and na_rep, max_rows and min_rows. – – smci Apr 22 '21 at 21:04
31

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
EdChum
  • 376,765
  • 198
  • 813
  • 562
  • 5
    This is a hack compared to the `to_string` answer. – ijoseph Apr 07 '20 at 22:04
  • This is a hack compared to [`to_string()`](https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.Series.to_string.html), which allows much more fine-grained control of name, index, header, also length, dtype, float_format and na_rep, max_rows and min_rows. – smci Apr 22 '21 at 21:03
3

Sometimes I do print(*s, sep='\n'):

s = pd.Series(['race', 'gender'], index=[311, 317])
print(*s, sep='\n')

gives

race
gender
user66081
  • 420
  • 7
  • 15