Skip to content

Add example to Dataframe.head() method's docstring. #18746

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 5 commits into from
Closed
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -3563,6 +3563,42 @@ def head(self, n=5):
-------
obj_head : type of caller
The first n rows of the caller object.

Examples
--------
>>> df = pd.DataFrame({
... 'col1' : ['A', 'A', 'B', np.nan, 'D', 'C', 'E', 'E', 'F'],
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove extra space after you finish the string:

'col1': 

Instead of:
'col1' :

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and being PEP8 compliant!

... 'col2' : [2, 1, 9, 8, 7, 4, 0, 2, 2],
... 'col3': [0, 1, 9, 4, 2, 3, 5, 7, 3],
... })
>>> df
col1 col2 col3
0 A 2 0
1 A 1 1
2 B 9 9
3 NaN 8 4
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are you adding a nan value?
This is creating a mixed type column. (floats and strings) is this intentional?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NaN is used for marking missing values in string (object) dtype columns too, so this is OK.

Copy link
Author

@dpshelio dpshelio Dec 12, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just copied from an example above, but I agree. It made sense in the sort_values, but not really here.

4 D 7 2
5 C 4 3
6 E 0 5
7 E 2 7
8 F 2 3

Viewing the first 5 lines (the default)
>>> df.head()
col1 col2 col3
0 A 2 0
1 A 1 1
2 B 9 9
3 NaN 8 4
4 D 7 2

Viewing the first n lines (three in this case)
>>> df.head(n=3)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You don't need to specify n
the same thing can be achieved with:

df.head(3)

col1 col2 col3
0 A 2 0
1 A 1 1
2 B 9 9

"""

return self.iloc[:n]
Expand Down