将重复索引的系列数据附加到pandas dataframe列

时间:2017-09-28 06:41:32

标签: python pandas numpy dataframe

我有一个名为result的系列,其中的数据使用numpy repeat函数复制5次。

result=np.repeat(rating_df['RESULT'],5)

结果系列看起来像重复索引。 results data 我想将结果系列数据添加到feature_file_df datframe

中的新列
feature_file_df_trans['result']=result

我收到此错误

alueError                                Traceback (most recent call last)
<ipython-input-150-cffb056edf1a> in <module>()
----> 1 feature_file_df_trans['result']=result

/home/jayashree/anaconda2/lib/python2.7/site-packages/pandas/core/frame.pyc in __setitem__(self, key, value)
   2427         else:
   2428             # set column
-> 2429             self._set_item(key, value)
   2430 
   2431     def _setitem_slice(self, key, value):

/home/jayashree/anaconda2/lib/python2.7/site-packages/pandas/core/frame.pyc in _set_item(self, key, value)
   2493 
   2494         self._ensure_valid_index(value)
-> 2495         value = self._sanitize_column(key, value)
   2496         NDFrame._set_item(self, key, value)
   2497 

/home/jayashree/anaconda2/lib/python2.7/site-packages/pandas/core/frame.pyc in _sanitize_column(self, key, value, broadcast)
   2643 
   2644         if isinstance(value, Series):
-> 2645             value = reindexer(value)
   2646 
   2647         elif isinstance(value, DataFrame):

/home/jayashree/anaconda2/lib/python2.7/site-packages/pandas/core/frame.pyc in reindexer(value)
   2635                     # duplicate axis
   2636                     if not value.index.is_unique:
-> 2637                         raise e
   2638 
   2639                     # other

ValueError: cannot reindex from a duplicate axis

如何将列添加到看起来像这样的数据框 resultant dataframe

1 个答案:

答案 0 :(得分:2)

我认为您可以将Series转换为values,然后添加到numpy array列:

注意 - 需要相同长度的输出numpy array作为追加列。

feature_file_df_trans['result']=np.repeat(rating_df['RESULT'].values,5)

样品:

rating_df = pd.DataFrame({'RESULT':[1,2,3]})
feature_file_df_trans = pd.DataFrame({'a':range(15)})

feature_file_df_trans['result']=np.repeat(rating_df['RESULT'].values,5)
print (feature_file_df_trans)
     a  result
0    0       1
1    1       1
2    2       1
3    3       1
4    4       1
5    5       2
6    6       2
7    7       2
8    8       2
9    9       2
10  10       3
11  11       3
12  12       3
13  13       3
14  14       3

更一般的解决方案如果长度不同,需要获得每个长度的最小值,并在Series构造函数中对其进行过滤:

rating_df = pd.DataFrame({'RESULT':[1,2,3,5,6,7]})
feature_file_df_trans = pd.DataFrame({'a':range(15)}, index = range(3, 18))

result = np.repeat(rating_df['RESULT'].values,5)
len1 = len(feature_file_df_trans.index)
print (len1)
15
len2 = len(result)
print (len2)
30
len_min = min(len1, len2)

feature_file_df_trans['result'] = pd.Series(result[:len_min], 
                                            index=feature_file_df_trans.index[:len_min])
print (feature_file_df_trans)
     a  result
3    0       1
4    1       1
5    2       1
6    3       1
7    4       1
8    5       2
9    6       2
10   7       2
11   8       2
12   9       2
13  10       3
14  11       3
15  12       3
16  13       3
17  14       3
rating_df = pd.DataFrame({'RESULT':[1,2]})
feature_file_df_trans = pd.DataFrame({'a':range(15)})

result = np.repeat(rating_df['RESULT'].values,5)
len1 = len(feature_file_df_trans.index)
print (len1)
15
len2 = len(result)
print (len2)
10
len_min = min(len1, len2)

feature_file_df_trans['result'] = pd.Series(result[:len_min], 
                                            index=feature_file_df_trans.index[:len_min])
print (feature_file_df_trans)
     a  result
0    0     1.0
1    1     1.0
2    2     1.0
3    3     1.0
4    4     1.0
5    5     2.0
6    6     2.0
7    7     2.0
8    8     2.0
9    9     2.0
10  10     NaN
11  11     NaN
12  12     NaN
13  13     NaN
14  14     NaN