Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 376
pam Avatar asked Apr 15 '15 08:04

pam


People also ask

How do I change the data type of a pandas series?

Change data type of a series in PandasUse a numpy. dtype or Python type to cast entire pandas object to the same type. Alternatively, use {col: dtype, …}, where col is a column label and dtype is a numpy. dtype or Python type to cast one or more of the DataFrame's columns to column-specific types.

How do I strip text in pandas?

strip() function is used to remove leading and trailing characters. Strip whitespaces (including newlines) or a set of specified characters from each string in the Series/Index from left and right sides. Equivalent to str. strip().

What does Dtypes do in pandas?

To check the data type in pandas DataFrame we can use the “dtype” attribute. The attribute returns a series with the data type of each column. And the column names of the DataFrame are represented as the index of the resultant series object and the corresponding data types are returned as values of the series object.

What does Dtype (' O ') mean?

It means: 'O' (Python) objects. Source. The first character specifies the kind of data and the remaining characters specify the number of bytes per item, except for Unicode, where it is interpreted as the number of characters. The item size must correspond to an existing type, or an error will be raised.


2 Answers

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 
like image 120
ALollz Avatar answered Sep 19 '22 15:09

ALollz


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 
like image 30
EdChum Avatar answered Sep 16 '22 15:09

EdChum