Sort (order) data frame rows by multiple columns

I want to sort a data frame by multiple columns. For example, with the data frame below I would like to sort by column ‘z’ (descending) then by column ‘b’ (ascending): dd <- data.frame(b = factor(c(“Hi”, “Med”, “Hi”, “Low”), levels = c(“Low”, “Med”, “Hi”), ordered = TRUE), x = c(“A”, “D”, “A”, “C”), y = … Read more

How do I get the row count of a Pandas DataFrame?

How do I get the number of rows of a pandas dataframe df? 1 15 For a dataframe df, one can use any of the following: len(df.index) df.shape[0] df[df.columns[0]].count() (== number of non-NaN values in first column) Code to reproduce the plot: import numpy as np import pandas as pd import perfplot perfplot.save( “out.png”, setup=lambda … Read more

Renaming column names in Pandas

How do I change the column labels of a pandas DataFrame from: [‘$a’, ‘$b’, ‘$c’, ‘$d’, ‘$e’] to [‘a’, ‘b’, ‘c’, ‘d’, ‘e’]. 3 32 RENAME SPECIFIC COLUMNS Use the df.rename() function and refer the columns to be renamed. Not all the columns have to be renamed: df = df.rename(columns={‘oldName1’: ‘newName1’, ‘oldName2’: ‘newName2’}) # Or … Read more