2015-08-14 12 views
6

पर पंक्ति जोड़ने पर ValueError मैं गतिशील रूप से डेटाफ्रेम में शामिल करने का प्रयास कर रहा हूं, लेकिन df.loc[count] = pandas.DataFrame(amounts).T पंक्ति में ValueError: Incompatible Indexer with Dataframe त्रुटि प्राप्त करें।डेटाफ्रेम

df = pandas.DataFrame(index=numpy.arange(0, 1), columns=required_indices_of_series) 
#This just creates a dataframe with the right columns, but with values I need to modify, which I aim to do below. 
print('1', df) 
count = 0 
for bond in bonds: 
    #Some stuff here to get the Series Object `amounts` which is irrelevant. 
    print('2', pandas.DataFrame(amounts).T) 
    df.loc[count] = pandas.DataFrame(amounts).T 
    count += 1 

print('1', df) रिटर्न:

 1983-05-15  1983-11-15  1984-05-15  1984-11-15 
      NaN   NaN    NaN    NaN 

print('2', pandas.DataFrame(amounts).T) रिटर्न:

 1983-05-15  1983-11-15  1984-05-15  1984-11-15 
      1   1    1    101 

उत्तर

1

आप इसे गलत तरीके से कर रहे हैं, तो आप एक और dataframe में एक पंक्ति के लिए एक DataFrame असाइन करने की कोशिश कर रहे हैं।

आपको दाईं ओर pandas.DataFrame(amounts).T.loc[<columnName>] का उपयोग करने की आवश्यकता है।

उदाहरण -

df = pandas.DataFrame(index=numpy.arange(0, 1), columns=required_indices_of_series) 
#This just creates a dataframe with the right columns, but with values I need to modify, which I aim to do below. 
print('1', df) 
count = 0 
for bond in bonds: 
    #Some stuff here to get the Series Object `amounts` which is irrelevant. 
    print('2', pandas.DataFrame(amounts).T) 
    df.loc[count] = pandas.DataFrame(amounts).T.loc[<column>] 
    count += 1 

उदाहरण/डेमो -

In [23]: df1.loc[0] = pd.DataFrame(s).T.loc['A'] 

In [24]: df1 
Out[24]: 
    0 1 
0 1 3 
1 NaN NaN 

In [25]: df = pd.DataFrame([[1,2],[3,4]],columns=['A','B']) 

In [26]: df 
Out[26]: 
    A B 
0 1 2 
1 3 4 

In [27]: df1 = pd.DataFrame(index = np.arange(0,1),columns = s.index) 

In [28]: df1 
Out[28]: 
    0 1 
0 NaN NaN 

In [29]: s = df['A'] 

In [30]: df1.loc[0] = pd.DataFrame(s).T 
--------------------------------------------------------------------------- 
ValueError        Traceback (most recent call last) 
<ipython-input-30-24065a81c953> in <module>() 
----> 1 df1.loc[0] = pd.DataFrame(s).T 

C:\Anaconda3\lib\site-packages\pandas\core\indexing.py in __setitem__(self, key, value) 
    113  def __setitem__(self, key, value): 
    114   indexer = self._get_setitem_indexer(key) 
--> 115   self._setitem_with_indexer(indexer, value) 
    116 
    117  def _has_valid_type(self, k, axis): 

C:\Anaconda3\lib\site-packages\pandas\core\indexing.py in _setitem_with_indexer(self, indexer, value) 
    495 
    496    elif isinstance(value, ABCDataFrame): 
--> 497     value = self._align_frame(indexer, value) 
    498 
    499    if isinstance(value, ABCPanel): 

C:\Anaconda3\lib\site-packages\pandas\core\indexing.py in _align_frame(self, indexer, df) 
    688    return df.reindex(idx, columns=cols).values 
    689 
--> 690   raise ValueError('Incompatible indexer with DataFrame') 
    691 
    692  def _align_panel(self, indexer, df): 

ValueError: Incompatible indexer with DataFrame 

In [31]: df1.loc[0] = pd.DataFrame(s).T.loc['A'] 

In [32]: df1 
Out[32]: 
    0 1 
0 1 3 
+0

धन्यवाद। मैंने 'df.loc [count] = pandas.DataFrame (amounts) किया था। .Tloc [required_indices_of_series]' लेकिन त्रुटि प्राप्त करें 'KeyError: datetime.date (1983-05-15) datetime.date (1983-11) में से कोई नहीं -15) datetime.date (1984-05-15) datetime.date (1984-11-15) [इंडेक्स] 'में हैं, जो मैंने सोचा था कि वे मेरे प्रश्न में देखे गए थे। – user131983

+0

आपको सूचकांक में कॉलम का उपयोग करने की आवश्यकता नहीं है, जो कॉलम नाम आप चाहते हैं उसे आपूर्ति करें। जब मैं कॉलम कहता हूं, मेरा मतलब है कि नया इंडेक्स, क्योंकि आप ट्रांसपोज़ कर रहे हैं, कॉलम इंडेक्स बन जाते हैं और इंडेक्स कॉलम बन जाता है –