Skip to content

Instantly share code, notes, and snippets.

@diije
Created August 6, 2021 13:12
Show Gist options
  • Save diije/0444be68c162e4ffefec8ddeaf90af99 to your computer and use it in GitHub Desktop.
Save diije/0444be68c162e4ffefec8ddeaf90af99 to your computer and use it in GitHub Desktop.
Display k first rows of Pandas dataframe and sum of other rows
# Number of rows to keep
k = 8
# Keep k first rows + transpose df
temp = df.head(k).T
# If df has more than k rows, sum remaining rows
if len(df) > k:
temp['Other rows'] = df.tail(len(df)-k).sum()
# Transpose back
temp = temp.T
temp
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment