Skip to content

Instantly share code, notes, and snippets.

@ajpen
Created August 6, 2020 19:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ajpen/ef5b893a1193cf036fc01374a415d4ab to your computer and use it in GitHub Desktop.
Save ajpen/ef5b893a1193cf036fc01374a415d4ab to your computer and use it in GitHub Desktop.
pandas cheatsheet

WHERE clause equivalent on dataframe

df[df['column_name'] == 'what to search for']

With AND and OR logical operators: df[df[('column_name'] == 'what to search for') & ('column_name'] == 'what to search for')]

df[df[('column_name'] == 'what to search for') | ('column_name'] == 'what to search for')]

LIKE clause equivalent

df[df['column_name'].str.contains('substring to look for')]

Iterating

List comprehensions > df.apply

[x[0], x[1],...x[n] for x in zip(df['col1'], df['col2'], ... df['coln'])]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment