属性错误: 'Series'对象没有属性'Year'



我使用的是一个数据帧,并将时间列转换为年和月,如下所示:

consumer_confidence = pd.read_csv('consumer_confidence.csv')
business_confidence = pd.read_csv('business_confidence.csv')
consumer_confidence['Year'] = pd.to_datetime(consumer_confidence['TIME']).dt.year
consumer_confidence['Month'] = pd.to_datetime(consumer_confidence['TIME']).dt.month
business_confidence['Year'] = pd.to_datetime(business_confidence['TIME']).dt.year
business_confidence['Month'] = pd.to_datetime(business_confidence['TIME']).dt.month
business_confidence = business_confidence.groupby('Year')['Value'].sum()
consumer_confidence = consumer_confidence.groupby('Year')['Value'].sum()

尝试.groupby((语句会导致以下错误:

AttributeError: 'Series' object has no attribute 'Year'

我不确定如何解决这个问题,因为"年"现在应该是数据帧中的一列。有人能解释一下我的错误吗?

您的代码(带有如下所示的一些示例输入(对我来说很好:

import pandas as pd
'''
consumer_confidence = pd.read_csv('consumer_confidence.csv')
business_confidence = pd.read_csv('business_confidence.csv')
'''
consumer_confidence = pd.DataFrame({'TIME':['2021-01-01', '2021-02-01', '2022-04-11', '2022-04-12'], 'Value':[1,2,3,4]})
business_confidence = pd.DataFrame({'TIME':['2020-01-01', '2021-02-01', '2022-04-11', '2022-04-12'], 'Value':[5,6,7,8]})
print(consumer_confidence)
print(business_confidence)
consumer_confidence['Year'] = pd.to_datetime(consumer_confidence['TIME']).dt.year
consumer_confidence['Month'] = pd.to_datetime(consumer_confidence['TIME']).dt.month
business_confidence['Year'] = pd.to_datetime(business_confidence['TIME']).dt.year
business_confidence['Month'] = pd.to_datetime(business_confidence['TIME']).dt.month
print(consumer_confidence)
print(business_confidence)
business_confidence = business_confidence.groupby('Year')['Value'].sum()
consumer_confidence = consumer_confidence.groupby('Year')['Value'].sum()
print(consumer_confidence)
print(business_confidence)

输出:

TIME  Value
0  2021-01-01      1
1  2021-02-01      2
2  2022-04-11      3
3  2022-04-12      4
TIME  Value
0  2020-01-01      5
1  2021-02-01      6
2  2022-04-11      7
3  2022-04-12      8
TIME  Value  Year  Month
0  2021-01-01      1  2021      1
1  2021-02-01      2  2021      2
2  2022-04-11      3  2022      4
3  2022-04-12      4  2022      4
TIME  Value  Year  Month
0  2020-01-01      5  2020      1
1  2021-02-01      6  2021      2
2  2022-04-11      7  2022      4
3  2022-04-12      8  2022      4
Year
2021    3
2022    7
Name: Value, dtype: int64
Year
2020     5
2021     6
2022    15
Name: Value, dtype: int64

相关内容

  • 没有找到相关文章

最新更新