Store today's date into today, and manually calculate customers' ages and store them in ages_manual. Find all rows of ba

动态 未结 0 187
小小草
小小草 LV1 2023年9月23日 23:40 发表
Here's the code to store today's date into `today`, manually calculate customers' ages and store them in `ages_manual`, find all rows of `banking` where the `age` column is equal to `ages_manual`, and then filter `banking` into `consistent_ages` and `inconsistent_ages`: ```python # Store today's date and find ages today = pd.to_datetime('today') ages_manual = today.year - banking['birth_date'].dt.year # Find rows where age column == ages_manual age_equ = banking['age'] == ages_manual # Store consistent and inconsistent data consistent_ages = banking[age_equ] inconsistent_ages = banking[~age_equ] # Print number of inconsistent ages print("Number of inconsistent ages: ", inconsistent_ages.shape[0]) ``` In this code, we first use the `pd.to_datetime()` function to get today's date and store it in the variable `today`. We then calculate the customers' ages by subtracting their birth year from the current year (`today.year`) and store the result in `ages_manual`. We create a boolean mask `age_equ` that checks if the `age` column is equal to `ages_manual`. We use this boolean mask to filter the `banking` DataFrame into two separate DataFrames: `consistent_ages` and `inconsistent_ages`. The `consistent_ages` DataFrame contains the rows where the `age` column is equal to `ages_manual`, while the `inconsistent_ages` DataFrame contains the rows where they are not equal. Finally, we print the number of inconsistent ages using the `shape` attribute of the `inconsistent_ages` DataFrame.
收藏(0)  分享
相关标签: 智能问答
问题没解决?让AI助手帮你作答 AI助手
0个回复
  • 消灭零回复