Skip to content

Instantly share code, notes, and snippets.

@joshua-lovelock
Last active August 13, 2018 03:11
Show Gist options
  • Save joshua-lovelock/0687cc589c950781d75c3113cf77d80d to your computer and use it in GitHub Desktop.
Save joshua-lovelock/0687cc589c950781d75c3113cf77d80d to your computer and use it in GitHub Desktop.
Personal cheat sheet for python commands

Virtual env

Install: pip install virtualenv

Create: virtualenv my_project

Select python version: virtualenv -p /usr/bin/python2.7 my_project

Activate (Use): source my_project/bin/activate

Dectivate (Exit): deactivate

OS

See working directory: os.listdir('./')

Change working directory: os.chdir('dir')

Add folder to path: sys.path.append('/path/to/library')

Pandas

import pandas as pd

Open: pd.read_csv(filename, header=True)

Write name.to_csv(filename, header=True)

Drop row: name.drop(['col1','col2'], inplace=True)

Drop column: name.drop(['col1','col2'], axis=1, inplace=True)

Join on column: result = data1.set_index('id').join(data2.set_index('id'))

Get unique values in column: data['col1'].unique()

Get frequency of values in a column: data['col1'].value_counts()

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