Python pandas fundamentals

I use pandas all the time when I am working on data projects. However I will never ever learn its syntax. I keep looking into the documentation and googling things like How to slice two columns? or How to drop rows with empty values? I am writing this post to keep all the answers in one place.

Slicing one column

I want to selecto one column from a dataframe

col1 = df.col1

Slicing multiple columns

I want to select two columns from a dataframe

df = df[['col1','col2']]

Get names of columns…

I want to know how the columns are named

print(df.columns.values)

…and assign names to columns

df.rename(columns={'$a': 'a', '$b': 'b'}, inplace=True)

Or

df.rename(columns={0: 'a', 1: 'b'}, inplace=True)

Drop rows containing NAs

df.dropna(inplace=True)

Reset the index

I want the default integer index

df.reset_index(drop=True)

Subset a df based on an iterable

Use df.isin(iterable) to subset a df.

df.loc[df.index.isin(iterable)]

Remember: make sure that both iterable and df.index have type int and not str! It works, but it is very slow.

Iterating over series/dfs

For dataframe:

for index, row in df.iterrows():
    print(index, row)

For series:

for index row in s.iteritems():
    print(index, row)