Mental model
The str accessor¶
When you run prism_df['AccidentDate'].str[:4]
-
Input Series: Starts with the
AccidentDatecolumn, 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}\] -
Access
.str: The operation is prepared to run against every element. - Apply
[:4]: Slicing is applied to each string:"2024-01-15"\(\rightarrow\)"2024""2023-11-01"\(\rightarrow\)"2023""2022-05-20"\(\rightarrow\)"2022"
- Output Series: A new pandas Series containing the extracted substrings (Years) is returned.