User0110101
User0110101

Reputation: 119

Pandas - Print a single row in the same format as .head()

I'm currently formatting my data and I would like to print out a single row from the dataframe. So far I've tried this:

df.iloc[202]

However, I realized after testing my data the output doesn't ideally display how I would like it to show as I want it similar to the output style from df.head().

My question therefore, is there a way to display a single row with the table style output in Pandas or not?

Upvotes: 0

Views: 1723

Answers (3)

aneroid
aneroid

Reputation: 15987

Your question is a bit unclear. My guess is you want to select just one row from your dataframe using df.iloc[202] but the output looks like this:

A    4
B    9
Name: 202, dtype: int64

While df.head(1) would look like this:

   A  B
0  4  9

You can get the head()/table style of the output by giving iloc a slice which includes just the one row you want:

df.iloc[202:203]

     A  B
202  4  9

The end of a slice is not included in the range and it doesn't throw an error if index 203 (or even 202) doesn't exist.

Upvotes: 0

Sachin Raghav
Sachin Raghav

Reputation: 450

Hi you can do it like this

df.head(202).tail(1)

I hope it will help you.

OR

And also you can use

df[206:207]

OR

df.iloc[[207]]

Upvotes: 2

Niels Henkens
Niels Henkens

Reputation: 2696

Probably not the nicest solution, but does this do what you want to achieve?

df.head(202).tail(1)

or flexibel:

df.head(i).tail(1)

Upvotes: 0

Related Questions