Chuck
Chuck

Reputation: 1293

how can I suppress the "dtype" line when printing a pandas dataframe?

I have a dataframe and need to print the output of some aggregations and such.

For example, a line in my script has:

pd.f_type = inventory['fruit'].value_counts()
print(f_type)

And its output like this:

apple         2
watermelon    1
pineapple     1
grapefruit    1
orange        1
Name: fruit, dtype: int64

How I can I remove the last line with "Name..."? I don't need it. I just need to see the actual output. Do I need to print with a for loop or something?

Upvotes: 5

Views: 4407

Answers (2)

VnC
VnC

Reputation: 2026

in your case you can also do:

print(pd.DataFrame(f_type))

The name and dtype underneath the data is printed out only for pandas.Series

Upvotes: 3

Milo
Milo

Reputation: 3318

If the removal of the last line is just for the sake of output, you can do like this:

f_type.to_string()

However, be careful with using .to_string() on large dataframes, because it will convert and print the entire dataframe, not just the amount of rows specified in pd.options.display.max_rows.

In addition to that, if you have a lot of columns the output will be pretty messed up. In that case you should probably do a print loop instead.

Upvotes: 8

Related Questions