Applying FunctionsΒΆ
One of the key concepts in Pandas is that you can apply functions to the data frame itself. This differs from how data manipulation is done usually in Python, where loops and new data structures is primarily how you end up operating.
# Load your dataframe
import pandas as pd
csv_url = "https://raw.githubusercontent.com/paiml/wine-ratings/main/wine-ratings.csv"
df = pd.read_csv(csv_url, index_col=0)
df.head()
We want to set the βtopβ wines given some arbitrary opinion on the rating. Wines are usually rated from 80 points to 100, and wines in the 90-94 point range are already good. So letβs set the business requirement to set anything that is 95 points or greater to be βgoodβ.
# the function can be a regular function or a headless one with lambda
def good_wine(value):
if value > 94:
return True
return False
df['good'] = df['rating'].apply(good_wine)
df.query('rating > 94').head()