将列中的值作为新列名称中的错误?
我要做的是在“年”内为每个“子市场”的平均“ 1_bed_effective_rent_per_unit_modified”。我平均每年的“子市场”平均四分之三 df = counter_rental_df.groupby([[“ Year”,“ submarkets”])[“ 1_bed_effective_rent_per_per_unit_modified”]。平均()
,并获得此输出为系列
年度 | submarkets | 1_bed_effective_effective_fective_rent_rent_per_per_per_unit_modified |
---|---|---|
2000 2000 | 1 ward | 1148.550 |
2 2683. 2001 | 2683.00 | |
2001 2001 2001年 | 1 Ward | 896.00 |
2 Ward | 2107.50 |
i然后使用此df_1 = df.to_frame()
年度
列仅在每年打印一次的年份中,如上表所示。
我想要的是我希望最终产品的外观是
子市场 | 2000 | 2001 |
---|---|---|
1 Ward | 1148.50 | 896.00 |
2 Ward | 2683 | 2107.50 |
,所以我使用了这条线df_1_2 = df_1.pivot(index ='submarkets' ='1_bed_effective_rent_per_unit_modified')\。reset_index()
,我遇到了一个错误“ keyError:'submarkets'”。如何解决此问题以获取所需的输出?
What I am trying to do produce the average "1_Bed_Effective_Rent_Per_Unit_Modified" for each "SubMarket" within a "Year". I took the average of four quarters in a year for each "SubMarket" with this linedf = county_rental_df.groupby(["Year", "SubMarkets"])["1_Bed_Effective_Rent_Per_Unit_Modified"].mean()
and got this output as a series
Year | SubMarkets | 1_Bed_Effective_Rent_Per_Unit_Modified |
---|---|---|
2000 | 1 Ward | 1148.50 |
2 Ward | 2683.00 | |
2001 | 1 Ward | 896.00 |
2 Ward | 2107.50 |
I then made the series into a data frame using this df_1 = df.to_frame()
The year column only has the year printed once for each year like shown in the above table.
What I want I want the final product to look like is
SubMarkets | 2000 | 2001 |
---|---|---|
1 Ward | 1148.50 | 896.00 |
2 Ward | 2683 | 2107.50 |
so I used this line df_1_2 = df_1.pivot(index='SubMarkets', columns='Year', values='1_Bed_Effective_Rent_Per_Unit_Modified')\.reset_index()
and I get an error "KeyError: 'SubMarkets'". How can I fix this to get my desired output?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要重置
df_1
的索引You need reset the index of
df_1
before pivot