My Python Cheat Sheet

These are some python code snippets that I use very often.

List Manipulation

Concatenate two python lists

 

Convert a python string to a list of characters

 

JSON Manipulation

Convert a dictionary to a json string

 

Convert a json string back to a python dictionary

 

Load a json file into a pandas data frame

 

DataFrame Manipulation

Group by a column and keep the column afterwards

 

Convert a dictionary to a pandas data frame

Let’s say you have a dict as follows:

my_dict={'mrr':0.4,'map':0.3,'precision':0.6}.

To convert this to a pandas Data Frame, you can do the following:

You will see the following output:

 

Select rows matching a specific column criteria

Let’s say you want to find rows where the column value matches a specific constraint. You could use the following:

 

Sort data frame by value

 

Create a new derived column in your data frame

The goal here is to create a new column with values populated based on the values of an old column. Let’s say you want a new column that adds 1 to a value from an old column.

 

Select specific columns from a data frame

 

System Commands

Run a system command from within Python code

 

File / Directory Operations

Safely create nested directories in Python

 

Evaluation

Compute per-class precision, recall, f1 scores

The goal here is to compute per-class precision, recall and f1 scores and display the results using a data frame.

The first step is to collect your labels as two separate lists. (1) the predicted labels and (2) the corresponding true labels. For example:

Once you have the true and predicted labels in a list, you can use sklearn’s precision_recall_fscore_support module to compute all the scores for you. Here’s how you do it:

Example output: