My Python Cheat Sheet

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

Topics include: list manipulation, json manipulation, data frame and etc.

Python 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

 

Pandas DataFrame Manipulation

Group by a column and keep the column afterwards

Convert a data frame to a list of dictionary values

Let’s say you want a list of dictionaries from a pandas data frame as follows:

From this:

To this:

This is the code you would use:

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:

 

Create a new data frame column with specific values

Let’s say you want to add an additional column to a data frame with values generated via some external processing. You can transform the external values into a list and do the following:

Sort data frame by value

Get unique values from a data frame column

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 /display 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: