Skip to content

adding left and right view to DataFrame, equivalent to head() and tail() #7005

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 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
18 changes: 18 additions & 0 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -1733,6 +1733,24 @@ def _getitem_frame(self, key):
raise ValueError('Must pass DataFrame with boolean values only')
return self.where(key)

def left(self, n=5):
"""
Return first n columns
"""
l = self.shape[1]
if l == 0 or n == 0:
return self
return self.iloc[:, :n]

def right(self, n=5):
"""
Return last n columns
"""
l = self.shape[1]
if l == 0 or n == 0:
return self
return self.iloc[:, -n:]

def query(self, expr, **kwargs):
"""Query the columns of a frame with a boolean expression.

Expand Down
2 changes: 1 addition & 1 deletion pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -1781,7 +1781,7 @@ def head(self, n=5):
Returns first n rows
"""
l = len(self)
if l == 0 or n==0:
if l == 0 or n == 0:
return self
return self.iloc[:n]

Expand Down
23 changes: 23 additions & 0 deletions pandas/tests/test_frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -4470,6 +4470,29 @@ def test_head_tail(self):
assert_frame_equal(empty_df.tail(), empty_df)
assert_frame_equal(empty_df.head(), empty_df)

def test_left_right(self):
assert_frame_equal(self.frame.left(), self.frame.iloc[:, :5])
assert_frame_equal(self.frame.right(), self.frame.iloc[:, -5:])
assert_frame_equal(self.frame.left(0), self.frame)
assert_frame_equal(self.frame.right(0), self.frame)
assert_frame_equal(self.frame.left(-1), self.frame.iloc[:, :-1])
assert_frame_equal(self.frame.right(-1), self.frame.iloc[:, 1:])
assert_frame_equal(self.frame.left(1), self.frame.iloc[:, :1])
assert_frame_equal(self.frame.right(1), self.frame.iloc[:, -1:])
# with a float index
df = self.frame.copy()
df.index = np.arange(len(self.frame)) + 0.1
assert_frame_equal(df.left(), df.iloc[:, :5])
assert_frame_equal(df.right(), df.iloc[:, -5:])
assert_frame_equal(df.left(0), df)
assert_frame_equal(df.right(0), df)
assert_frame_equal(df.left(-1), df.iloc[:, :-1])
assert_frame_equal(df.right(-1), df.iloc[:, 1:])
#test empty dataframe
empty_df = DataFrame()
assert_frame_equal(empty_df.right(), empty_df)
assert_frame_equal(empty_df.left(), empty_df)

def test_insert(self):
df = DataFrame(np.random.randn(5, 3), index=np.arange(5),
columns=['c', 'b', 'a'])
Expand Down