Can't get the right answer with a different approach

Screen Link:
https://app.dataquest.io/m/466/fuzzy-language-in-data-science/4/churned-customers

My Code:

import pandas as pd
import datetime as dt

data = pd.read_csv("rfm_xmas19.txt", parse_dates=["trans_date"])
group_by_customer = data.groupby("customer_id")
last_transaction = group_by_customer["trans_date"].max()

best_churn = pd.DataFrame(last_transaction)

cutoff_day = dt.datetime(2019, 10, 16)

for date in best_churn["trans_date"]:
    if date < cutoff_day:
        best_churn["churned"] = 1
    else:
        best_churn["churned"] = 0

best_churn["churned"].value_counts()

What I expected to happen:
To get a column “churned” with 1’s or 0’s

What actually happened:

All rows are 0's

I know the exercise asks to apply a function but I wanted to try a for loop, problem is that is not working, always returns 0, even when there are rows when should be 1. It has to be a simple mistake but I can’t seem to find it…

Hey. Let me try to nudge you in the right direction by using a similar technique on a small dataset.

We first load the data, don’t worry if you don’t understand this code, it’s not important.

>>> import seaborn as sns
>>> tips = sns.load_dataset("tips").head()
>>> tips
   total_bill   tip     sex smoker  day    time  size
0       16.99  1.01  Female     No  Sun  Dinner     2
1       10.34  1.66    Male     No  Sun  Dinner     3
2       21.01  3.50    Male     No  Sun  Dinner     3
3       23.68  3.31    Male     No  Sun  Dinner     2
4       24.59  3.61  Female     No  Sun  Dinner     4

Now we’ll check the effect of running tips["size"] = 1.

>>> tips["size"] = 1
>>> tips
   total_bill   tip     sex smoker  day    time  size
0       16.99  1.01  Female     No  Sun  Dinner     1
1       10.34  1.66    Male     No  Sun  Dinner     1
2       21.01  3.50    Male     No  Sun  Dinner     1
3       23.68  3.31    Male     No  Sun  Dinner     1
4       24.59  3.61  Female     No  Sun  Dinner     1

It modified the whole column!

Let’s now mimic the technique you used.

>>> for k in range(10): tips["size"] = k
>>> tips
   total_bill   tip     sex smoker  day    time  size
0       16.99  1.01  Female     No  Sun  Dinner     9
1       10.34  1.66    Male     No  Sun  Dinner     9
2       21.01  3.50    Male     No  Sun  Dinner     9
3       23.68  3.31    Male     No  Sun  Dinner     9
4       24.59  3.61  Female     No  Sun  Dinner     9

Do you see what’s happening here?

If you want to iterate over the rows, I recommend using pandas.DataFrame.iterrows.

Thanks Bruno! I guess I need to refresh some Python concepts.