Q:编写一个函数,将房屋数据转换为四分之一,然后将其返回为均值 数据框中的值。此数据框应为带有 从2000q1到2016q3的列,并且应具有多索引 格式为[“ State”,“ RegionName”]。
我已经过滤掉了未使用的列,下面是我尝试过的代码:
def quater(date):
return str(date.year)+'q'+str(date.quarter)
def convert_housing_data_to_quarters():
house = pd.read_csv('City_Zhvi_AllHomes.csv')
cols = house.columns.values
house.drop(["RegionID", "Metro", "CountyName", "SizeRank"] ,axis=1,inplace=True)
house.groupby(columns=[quater(pd.Timestamp(x)) for x in cols[2:]], axis=1,inplace = True)
return house
convert_housing_data_to_quarters()
我遇到以下错误:
ValueError Traceback (most recent call last)
pandas/tslib.pyx in pandas.tslib.convert_str_to_tsobject (pandas/tslib.c:29408)()
pandas/src/datetime.pxd in datetime._string_to_dts (pandas/tslib.c:95676)()
ValueError: Error parsing datetime string "State" at position 0
During handling of the above exception, another exception occurred:
ValueError Traceback (most recent call last)
pandas/tslib.pyx in pandas.tslib.convert_str_to_tsobject (pandas/tslib.c:29833)()
pandas/tslib.pyx in pandas.tslib.parse_datetime_string (pandas/tslib.c:35434)()
/opt/conda/lib/python3.6/site-packages/dateutil/parser.py in parse(timestr, parserinfo, **kwargs)
1181 else:
-> 1182 return DEFAULTPARSER.parse(timestr, **kwargs)
1183
/opt/conda/lib/python3.6/site-packages/dateutil/parser.py in parse(self, timestr, default, ignoretz, tzinfos, **kwargs)
558 if res is None:
--> 559 raise ValueError("Unknown string format")
560
ValueError: Unknown string format
During handling of the above exception, another exception occurred:
ValueError Traceback (most recent call last)
<ipython-input-8-c6e65688298c> in <module>()
18 return house
19
---> 20 convert_housing_data_to_quarters()
<ipython-input-8-c6e65688298c> in convert_housing_data_to_quarters()
15 cols = house.columns.values
16 house.drop(["RegionID", "Metro", "CountyName", "SizeRank"] ,axis=1,inplace=True)
---> 17 house.groupby(columns=[quater(pd.Timestamp(x)) for x in cols[2:]], axis=1,inplace = True)
18 return house
19
<ipython-input-8-c6e65688298c> in <listcomp>(.0)
15 cols = house.columns.values
16 house.drop(["RegionID", "Metro", "CountyName", "SizeRank"] ,axis=1,inplace=True)
---> 17 house.groupby(columns=[quater(pd.Timestamp(x)) for x in cols[2:]], axis=1,inplace = True)
18 return house
19
pandas/tslib.pyx in pandas.tslib.Timestamp.__new__ (pandas/tslib.c:10447)()
pandas/tslib.pyx in pandas.tslib.convert_to_tsobject (pandas/tslib.c:27555)()
pandas/tslib.pyx in pandas.tslib.convert_str_to_tsobject (pandas/tslib.c:29886)()
ValueError: could not convert string to Timestamp
这里假定日期为quater的参数是我假定的时间戳。 我想要一种合并季度中的列的方法。 请帮助我,谢谢您的时间。