Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/310.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 当我有FY和FQ字符串时,创建财政年度(FY)、财政季度(FQ)时间序列_Python_Pandas_Time Series - Fatal编程技术网

Python 当我有FY和FQ字符串时,创建财政年度(FY)、财政季度(FQ)时间序列

Python 当我有FY和FQ字符串时,创建财政年度(FY)、财政季度(FQ)时间序列,python,pandas,time-series,Python,Pandas,Time Series,我的数据框架中有两个特性,分别是财政年度(FY)和财政季度(FQ)的字符串: 我使用了以下方法: index=pd.PeriodIndex(data.FY, data.fq, freq='Q') data['index']=index 我的输出如下: index 2008Q1 2009Q1 2009Q1 2010Q1 我期待着这样的事情: index 2008Q3 2009Q4 2009Q1 2010Q2 它不起作用的原因是PeriodIndex需要一

我的数据框架中有两个特性,分别是财政年度(FY)和财政季度(FQ)的字符串:

我使用了以下方法:

index=pd.PeriodIndex(data.FY, data.fq, freq='Q')  
data['index']=index
我的输出如下:

index  
2008Q1  
2009Q1  
2009Q1  
2010Q1
我期待着这样的事情:

index  
2008Q3  
2009Q4  
2009Q1  
2010Q2 

它不起作用的原因是PeriodIndex需要一个字符串列表来描述所需的所有索引。您可以提供必要的列表和列表理解

创建数据:

data = { 'years': ['2008', '2009', '2010', '2011'], 'Q':['3', '4', '1', '2']}
df = pd.DataFrame(data)


   Q years
0  3  2008
1  4  2009
2  1  2010
3  2  2011
使用列表理解创建索引并应用它:

index = pd.PeriodIndex([y+'Q'+q for y, q in zip(df.years, df.Q)], freq='Q')
df.index = index


        Q years
2008Q3  3  2008
2009Q4  4  2009
2010Q1  1  2010
2011Q2  2  2011

注意:您应该看看,它使用起来更简单。

谢谢!我要试试这个。
index = pd.PeriodIndex([y+'Q'+q for y, q in zip(df.years, df.Q)], freq='Q')
df.index = index


        Q years
2008Q3  3  2008
2009Q4  4  2009
2010Q1  1  2010
2011Q2  2  2011