簡體   English   中英

將具有重復索引的系列數據追加到pandas數據框列

[英]Appending series data with repeated index to pandas dataframe column

我有一個名為result的序列,並且使用numpy repeat函數將其中的數據復制了5次。

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

結果序列看起來像這樣,具有重復的索引。 結果數據 我想將結果系列數據添加到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

如何將列添加到如下所示的數據框中 結果數據框

我認為您可以將Seriesvalues ,然后添加到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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM