python-3.x - 'DatetimeIndex' 对象没有属性 'Date'
问题描述
我尝试通过聚合收入来生成每月 KPI 数据帧,但出现此错误:
AttributeError:“DatetimeIndex”对象没有属性“Year_Lease_Start”
这是数据框的格式:
VEHICLE_TYPE MARQUE COMPANY_TYPE COMPANY_NAME COMPANY_CODE PERSONAL SALESCHANNEL_SUB_CODE COMP_SEG_SUB_CODE BELONGS_UNDER_HIERARCHY_CODE DESCRIPTION CREATED_BY DATE_CREATED ACCESS_SET LEASE_ID TERM DISTANCE LEASE_START LEASE_END LEASE_EXTENDED_TO STATUS QUOTE_ID QUOTE_REVISION TITLE_NAME LOYER_FINANCIER_HT LOYER_FINANCIER_TTC PRESTATION_HT PRESTATION_TTC PRIX LOYER_TOTAL_HT Year_Lease_Start Month_Lease_Start YearMonth_Lease_Start
Date
2018-09-05 NI rgrgCUS lhkv erge 506183 Y DIRECT EPE 002053679_HR YHE 179868.0 2019-01-23 13:27:39.0 AG_PRime 385590 36 45000 2018-09-05 2021-09-04 00:00:00.0 NaN ACTIVE 3269111 1 Adooper 217.42 260.904 42.57 51.084 35500.0 259.99 2018 9 20180
任何想法请解决这个问题?谢谢
%pylab inline
import warnings
warnings.filterwarnings('ignore')
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
sns.set(style="white", color_codes=True)
# Transforming Lease_start_Date column to datetime type and mapping to a new columne as ILease_start_Date
DIRECT_PART_df.LEASE_START= pd.to_datetime(DIRECT_PART_df.LEASE_START)
# Construct Year, Month and YearMonth from Lease Start Date field
DIRECT_PART_df['Year_Lease_Start'], DIRECT_PART_df['Month_Lease_Start'] = DIRECT_PART_df['LEASE_START'].dt.year, DIRECT_PART_df['LEASE_START'].dt.month
DIRECT_PART_df['YearMonth_Lease_Start'] = DIRECT_PART_df['LEASE_START'].map(lambda x: 100*x.year + x.month)
# Create "Date" column in datetime format to use for index
DIRECT_PART_df['Date'] = pd.to_datetime(DIRECT_PART_df.LEASE_START.dt.date)
DIRECT_PART_df.set_index('Date', inplace=True)
# 1. Revenue - Generate Monthly KPIs DataFrame by aggregating Revenue
m_kpis = pd.DataFrame(DIRECT_PART_df.groupby([DIRECT_PART_df.index.Year_Lease_Start, DIRECT_PART_df.index.Year_Lease_Start.Month_Lease_Start])['LOYER_TOTAL_HT'].sum())
解决方案
问题在最后一行,列名使用字符串,而不是DIRECT_PART_df.index.Year_Lease_Start
:
m_kpis = DIRECT_PART_df.groupby(['Year_Lease_Start', 'Month_Lease_Start'], as_index=False)['LOYER_TOTAL_HT'].sum()
如果需要还通过索引:
m_kpis = DIRECT_PART_df.groupby([DIRECT_PART_df.index.year.rename('Year'), DIRECT_PART_df.index.month.rename('Month'), as_index=False)['LOYER_TOTAL_HT'].sum()
推荐阅读
- regex - 如何使用正则表达式选择包含特定单词的多行?
- android - WorkManager 不适用于百度推送 SDK
- lua - lua 打开带有表格的文件并更改某些值
- gradle - 跳过任务,因为它没有源文件,也没有以前的输出文件
- asp.net - 如何使用 aspnet 2.0 发布到 API
- javascript - 由于未知原因,React 钩子无法使用 setState
- c - 如何在 Xcode 的一个输出中组合函数
- python - 未调用 Django REST Framework 视图权限
- mysql - 如何在 SQL 上的 JSON 数组中搜索
- c# - 如何正确地将接收到的串行数据解析成行?