ValueError:列重叠但未指定后缀:Index(['Week_0'],dtype ='object')

时间:2019-04-05 12:26:39

标签: python pandas dataframe

data = data.join(pd.DataFrame(predicted_order,index=data.index).add_prefix('Week_'))

print(data)

data.to_csv('smallys_ARMA.csv', encoding='utf-8', index=False)

首次执行后,将Week_0新列添加到数据框。第二次执行后,出现以下错误:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-61-adcd0043cad7> in <module>()
      2 
      3 #data['Week_{}'.format(i)] = predicted_order
----> 4 data = data.join(pd.DataFrame(predicted_order,index=data.index).add_prefix('Week_'))
      5 
      6 #data = data.iloc[:, len(data)].add_prefix('Week_')

~/anaconda3/envs/tensorflow/lib/python3.5/site-packages/pandas/core/frame.py in join(self, other, on, how, lsuffix, rsuffix, sort)
   5314         # For SparseDataFrame's benefit
   5315         return self._join_compat(other, on=on, how=how, lsuffix=lsuffix,
-> 5316                                  rsuffix=rsuffix, sort=sort)
   5317 
   5318     def _join_compat(self, other, on=None, how='left', lsuffix='', rsuffix='',

~/anaconda3/envs/tensorflow/lib/python3.5/site-packages/pandas/core/frame.py in _join_compat(self, other, on, how, lsuffix, rsuffix, sort)
   5329             return merge(self, other, left_on=on, how=how,
   5330                          left_index=on is None, right_index=True,
-> 5331                          suffixes=(lsuffix, rsuffix), sort=sort)
   5332         else:
   5333             if on is not None:

~/anaconda3/envs/tensorflow/lib/python3.5/site-packages/pandas/core/reshape/merge.py in merge(left, right, how, on, left_on, right_on, left_index, right_index, sort, suffixes, copy, indicator, validate)
     56                          copy=copy, indicator=indicator,
     57                          validate=validate)
---> 58     return op.get_result()
     59 
     60 

~/anaconda3/envs/tensorflow/lib/python3.5/site-packages/pandas/core/reshape/merge.py in get_result(self)
    586 
    587         llabels, rlabels = items_overlap_with_suffix(ldata.items, lsuf,
--> 588                                                      rdata.items, rsuf)
    589 
    590         lindexers = {1: left_indexer} if left_indexer is not None else {}

~/anaconda3/envs/tensorflow/lib/python3.5/site-packages/pandas/core/internals.py in items_overlap_with_suffix(left, lsuffix, right, rsuffix)
   5024         if not lsuffix and not rsuffix:
   5025             raise ValueError('columns overlap but no suffix specified: %s' %
-> 5026                              to_rename)
   5027 
   5028         def lrenamer(x):

ValueError: columns overlap but no suffix specified: Index(['Week_0'], dtype='object')

我想要的是生成这种格式的动态列名:Week_i,其中i is a positive integer。每次执行之后,我想向数据框添加一个新列。该代码将运行未知次数,并且可能会在今天执行一次。那么下一次执行可能是2天之后。每次新生成的列应获得一个动态生成的名称。

0 个答案:

没有答案