Karl SvenssonBlogLinkedIn

Pandas Cheet Sheet

Written 2021-10-24 by Kalle
1 min read | 170 words

On the occasional times when I need to load and analyze data, Pandas is my go-to tool. The process usually starts with one or several .csv files that need to be loaded into memory, and continues with various steps of wrangling. I do this process seldom enough that I have to re-google the same things over and over. This post is a collection of Pandas related commands that I need to use every time.

Load csv

data = pd.read_csv(filename) # Standard way
data = pd.read_csv(filename, sep=';') # Set custom column delimiter
data = pd.read_csv(filename, names=['x', 'y', 'z']) # Explicitly set header

Print DataFrame

data.head() # First 5 rows
data.tail() # Last 5 rows

Get shape

shape = data.shape # (num_rows, num_cols)
num_rows = len(data) # num_rows

Add time-column

data['t'] = range(0, len(data))

Filter on time-column

data = data[data['t'] > t_low]
data = data[data['t'] < t_high]

Access data by column

data_x = data['x']

© 2024