将pandas._period.Period类型的列名称转换为小写 [英] Convert pandas._period.Period type Column names to Lowercase

查看:327
本文介绍了将pandas._period.Period类型的列名称转换为小写的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个具有列名的数据集城市"

I have a dataset 'City' with Column names

2000-01,2000-02,2000-03,2000-04,2000-05,......,2010-08,2010-09,2010-10,2010-11,2010-12.

我使用以下代码,并将列名命名为

I used the following code and got the column names as

2000Q1 , 2000Q2, 2000Q3, ......, 2010Q4

在pandas._period.Period数据类型中.

in pandas._period.Period datatype.

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'.

I want lowercase 'q' in my output column names.

谢谢.

推荐答案

您需要strftime适用于PeriodIndex的内容:

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

这篇关于将pandas._period.Period类型的列名称转换为小写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆