我有一个带有列名的数据集‘City
2000-01,2000-02,2000-03,2000-04,2000-05,......,2010-08,2010-09,2010-10,2010-11,2010-12.
我使用以下代码获取列名,如下所示
2000Q1 , 2000Q2, 2000Q3, ......, 2010Q4
在pandas._period.Period数据类型中。
def Problem():
hd = pd.read_csv('City.csv')
hd = hd.groupby(pd.PeriodIndex(hd.columns, freq='Q'), axis =1).mean()
return hd
Problem()
我希望这些列是
2000q1, 2000q2, 2000q3, ....... ,2010q4
我想在我的输出列名中使用小写的'q‘。
谢谢。
发布于 2017-02-20 02:04:26
您需要与PeriodIndex
一起工作的strftime
hd.columns = hd.columns.strftime('%Yq%q')
示例:
hd = pd.DataFrame({'2000-01':[1,3], '2000-05':[5,6]})
hd = hd.groupby(pd.PeriodIndex(hd.columns, freq='Q'), axis =1).mean()
print (hd)
2000Q1 2000Q2
0 1 5
1 3 6
hd.columns = hd.columns.strftime('%Yq%q')
print (hd)
2000q1 2000q2
0 1 5
1 3 6
https://stackoverflow.com/questions/42330848
复制相似问题