A few pandas tricks I reach for often when doing data analysis: finding unique values, filtering rows, grouping data, and saving results back to CSV.
- Finding unique values
import pandas as pd
data = pd.read_csv('https://gist.githubusercontent.com/tiangechen/b68782efa49a16edaf07dc2cdaa855ea/raw/0c794a9717f18b094eabab2cd6a6b9a226903577/movies.csv')
data.Film.unique()This will print only unique values in Film column in the csv.
- Filtering Data
Say you only want movies with an audience score above 50 that are comedies. Filtering handles this in one line.
new_data = data[(data['Audience score %'] > 50) & (data.Genre == 'Comedy')]- Saving to
csv.
Pandas has a function that saves data to a csv file. For instance, to save all the unique movie names, first convert them to a data frame:
uniq = data.Film.unique()
out = pd.DataFrame(uniq)
out.to_csv('uniq.csv')This will create a csv file of unique names.
Groupby
Groups data by a column. For instance, to count movies by genre, use groupby.
data.groupby('Genre').Film.agg(['count'])This outputs the total number of movies for each genre. You can also use other aggregations like sum, mean, and median.
- String Operations
You can also use string operations when working with text data.
# lower case a specific column
data['Genre'] = data['Genre'].str.lower()This lowercases the Genre column. You can also use upper() for uppercase, or apply your own regex with replace.
Let me know in the comments what your favorite pandas trick is.