Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/287.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 合并时间序列变量以创建具有任意索引的新数据帧的问题_Python_Join_Merge_Pandas - Fatal编程技术网

Python 合并时间序列变量以创建具有任意索引的新数据帧的问题

Python 合并时间序列变量以创建具有任意索引的新数据帧的问题,python,join,merge,pandas,Python,Join,Merge,Pandas,因此,我尝试合并以下数据列,这些数据列当前被索引为每日条目(但每周只有一次点数)。我已经将这些列分成了年份变量,但是我很难将它们放入一个组合数据框中,并且忽略了日期索引,这样我就可以在这些年中按周构建最小/最大列。我不知道如何让merge/join函数执行此操作 #创建年份变量,用新索引附加到新数据框 我有以下资料: def minmaxdata(): Totrigs = dataforgraphs() tr = Totrigs yrs=[tr['2007'],tr['20

因此,我尝试合并以下数据列,这些数据列当前被索引为每日条目(但每周只有一次点数)。我已经将这些列分成了年份变量,但是我很难将它们放入一个组合数据框中,并且忽略了日期索引,这样我就可以在这些年中按周构建最小/最大列。我不知道如何让merge/join函数执行此操作

#创建年份变量,用新索引附加到新数据框

我有以下资料:

def minmaxdata():
   Totrigs =   dataforgraphs()
   tr = Totrigs
   yrs=[tr['2007'],tr['2008'],tr['2009'],tr['2010'],tr['2011'],tr['2012'],tr['2013'],tr['2014']]
   yrlist = ['tr07','tr08','tr09','tr10','tr11','tr12','tr13','tr14']
   dic = dict(zip(yrlist,yrs))
   yr07,yr08,yr09,yr10,yr11,yr12,yr13,yr14    =dic['tr07'],dic['tr08'],dic['tr09'],dic['tr10'],dic['tr11'],dic['tr12'],dic['tr13'],dic['tr14']


   minmax = yr07.append([yr08,yr09,yr10,yr11,yr12,yr13,yr14],ignore_index=True)

I would like a Dataframe like the following:

   2007  2008 2009 2010 2011 2012 2013 2014  min max 

1   10    13   10    12   34  23   22   14   10  34  
2   25    ...
3   22
4   ...
5
.
.
.  ...
52

我不确定您的原始数据是什么样子,但我认为多年来硬编码不是一个好主意。你失去了可重用性。我将设置一个按日期索引的随机整数序列,每周有一个日期

In [65]: idx = pd.date_range ('2007-1-1','2014-12-31',freq='W')

In [66]: df = pd.DataFrame(np.random.randint(100, size=len(idx)), index=idx, columns=['value']) 

In [67]: df.head()
Out[67]: 
            value
2007-01-07      7
2007-01-14      2
2007-01-21     85
2007-01-28     55
2007-02-04     36

In [68]: df.tail()
Out[68]: 
            value
2014-11-30     76
2014-12-07     34
2014-12-14     43
2014-12-21     26
2014-12-28     17
然后获取本周的
年份

In [69]: df['year'] = df.index.year

In [70]: df['week'] = df.groupby('year').cumcount()+1
(你可以在一周内尝试
df.index.week
,但我见过一些奇怪的行为,比如从1月的第53周开始)

最后,做一个透视表来转换并获得行的最大/最小值:

In [71]: df2 = df.pivot_table(index='week', columns='year', values='value')

In [72]: df2['max'] = df2.max(axis=1)

In [73]: df2['min'] = df2.min(axis=1)
现在,我们的数据帧df2看起来像这样,应该是您所需要的:

In [74]: df2
Out[74]: 
year  2007  2008  2009  2010  2011  2012  2013  2014  max  min
week                                                          
1        7    82    13    32    24    58    18    10   82    7
2        2     5    29     0     2    97    59    83   97    0
3       85    89     8    83    63    73    47    49   89    8
4       55     5     1    44    78    10    13    87   87    1
5       36    41    48    98    98    24    24    69   98   24
6       51    43    62    60    44    57    34    33   62   33
7       37    66    72    46    28    11    73    36   73   11
8       30    13    86    93    46    67    95    15   95   13
9       78    84    16    21    70    39    43    90   90   16
10       9     2    88    15    39    81    44    96   96    2
11      34    76    16    44    44    26    30    77   77   16
12       2    24    23    13    25    69    25    74   74    2
13      66    91    67    77    18    47    95    66   95   18
14      59    52    22    42    40    99    88    21   99   21
15      76    17    31    57    43    31    91    67   91   17
16      76    38    53    43    84    45    78     9   84    9
17      88    53    34    22    99    93    61    42   99   22
18      78    19    82    19     5    80    55    69   82    5
19      54    92    56     6     2    85     7    67   92    2
20       8    56    86    41    60    76    31    81   86    8
21      64    76    11    38    41    98    39    72   98   11
22      21    86    34     1    15    27    26    95   95    1
23      82    90     3    17    62    18    93    20   93    3
24      47    42    32    27    83     8    22    14   83    8
25      15    66    70    16     4    22    26    14   70    4
26      12    68    21     7    86     2    27    10   86    2
27      85    85     9    39    17    94    67    42   94    9
28      73    80    96    49    46    23    69    84   96   23
29      57    74     6    71    79    31    79     7   79    6
30      18    84    85    34    71    69     0    62   85    0
31      24    40    93    53    72    46    44    71   93   24
32      95     4    58    57    68    27    95    71   95    4
33      65    84    87    41    38    45    71    33   87   33
34      62    14    41    83    79    63    44    13   83   13
35      49    96    50    62    25    45    69    63   96   25
36       6    38    86    34    98    60    67    80   98    6
37      99    44    26    19    19    20    57    17   99   17
38       2    40     7    65    68    58    68    13   68    2
39      72    31    83    65    69    39    10    76   83   10
40      90    31    42    20     7     8    62    79   90    7
41      10    46    82    96    30    43    12    84   96   10
42      79    38    28    78    25     9    80     2   80    2
43      64    83    63    40    29    86    10    15   86   10
44      89    91    62    48    53    69    16     0   91    0
45      99    26    85    45    26    53    79    86   99   26
46      35    14    46    25    74     6    68    44   74    6
47      17     9    84    88    29    83    85     1   88    1
48      18    69    55    16    77    35    16    76   77   16
49      60     4    36    50    81    28    50    34   81    4
50      36    29    38    28    81    86    71    43   86   28
51      41    82    95    27    95    77    74    26   95   26
52       2    81    89    82    28     2    11    17   89    2
53     NaN   NaN   NaN   NaN   NaN     0   NaN   NaN    0    0
编辑: 如果需要对特定列执行
max/min
,只需列出它们即可。在本例中(2007-2013),它们是连续的,因此您可以执行以下操作

df2['max_2007to2013'] = df2[range(2007,2014)].max(axis=1)

如果没有,只需列出如下内容:
df2[[2007201020122013]].max(axis=1)

谢谢!我是在意识到自己犯了一个错误后得到的。作为最后一个后续问题,是否可以使用min/max列设置min/max中包含的年份。就我而言,我只想包括2007-2013年,而不是2014年。我非常感谢您在这方面的帮助。如果你能看到我上面的问题,我会帮你完成最后一部分。只是想知道如何索引特定年份的最小/最大值。他们尝试了您建议的两种方法,但得到了“关键错误范围(20072013)”,不确定您是否可以列出它们。你知道为什么吗?你的列名可能是
str
而不是
int
,也就是说,“2007”不是2007。尝试
df[['2007','2008','2009']]
尝试了一下,我得到了错误:keyrerror:“['2007''2008''2009''2010''2011''2012''2013']不在索引中”。当我使用额外的括号时,我得到了一个错误:KeyError:'键长度(7)大于多索引lexsort深度(2)'