Mental model

The str accessor

When you run prism_df['AccidentDate'].str[:4]

  1. Input Series: Starts with the AccidentDate column, which is a pandas Series of strings.

    \[\begin{array}{|c|c|} \hline \text{Index} & \text{AccidentDate} \\ \hline 0 & \text{"2024-01-15"} \\ 1 & \text{"2023-11-01"} \\ 2 & \text{"2022-05-20"} \\ \hline \end{array}\]
  2. Access .str: The operation is prepared to run against every element.

  3. Apply [:4]: Slicing is applied to each string:
    • "2024-01-15" \(\rightarrow\) "2024"
    • "2023-11-01" \(\rightarrow\) "2023"
    • "2022-05-20" \(\rightarrow\) "2022"
  4. Output Series: A new pandas Series containing the extracted substrings (Years) is returned.

Comments