2015-05-27 71 views
1

我一直在熊貓收到此錯誤:數據框中創作 - 重建索引

raise Exception('Reindexing only valid with uniquely valued Index ' 
Exception: Reindexing only valid with uniquely valued Index objects 

我的理解,如果列名是相同的,而試圖從字典做出DataFrame會發生。

但是,我的不是。

這裏是我使用的字典 - 這看起來是這樣的 - 很喜歡,在教程中給出,不工作:

d = {'Sample1': 4.121025 0.306828 
4.119957 0.335473 
4.096208 0.331831 
... 
0.824808 0.366679 
0.745721 0.586742 
Length: 406, 'Sample2': 3.444444 0.937468 
3.315508 0.855920 
... 
0.928144 0.236640 
0.918519 0.232346 
Length: 991} 

這是我使用生成該字典的代碼中,些吧細節是不相關的,但spec.Y和spec.X只是numpy的數組:

d = dict() 
for tab in self.tab_list: 
    spec = tab.temp_spectra 
    name = str(spec.spectra_name) 
    d[name] = pd.Series(spec.Y, index=spec.X) 
    print(name) 
print(d) 
df = pd.DataFrame(d) 
print(df) 

任何想法,爲什麼這個錯誤可能會被顯示出來,所以我可以知道後把去什麼?列名似乎並不是這樣,而且我是在假設索引可以匹配或者不匹配的情況下運行的 - 這應該不重要 - 這就是爲什麼我使用這個包的原因。我想要'hashtable'類型的感覺,X數組可以是不同的或相同的,並且重疊會將值放在同一行中,或者在需要時創建一個新行,以便它們可以一起查看。

編輯:

我想通了,該解決方案必須在我的數據集之一(我的數據集的一個索引重複的項目做的是建立在plotly通過手動跟蹤,並因此得到了一個X值的兩個Y值)。 我會在我自己的問題的答案中舉幾個例子。

+0

你可以提供一個可重複的例子,顯示了錯誤? – joris

+0

謝謝你的評論!我通常會準備一個小的可編譯樣本來嘗試重現錯誤,但是這次我無法弄清楚什麼是錯誤的,因爲我編寫的小型可編譯腳本完全可以工作。因此,這是我的具體數據類型或某些謎團,我還不知道。我希望有人可能對這類問題有經驗或見解,以提出建議。我現在添加一個小的可編譯腳本。 – chase

回答

1

總是檢查一個X值是否有2個Y值,或者是一個數據幀的每個索引有兩個值。 我得到的數據集的一個手工跟蹤在圖上plotly和創建數據的方法,其得到2個y值用於一個X.

爲了說明: 下面是一個小的腳本是可編譯:

import pandas as pd 
import numpy as np 

def f(i): 
    return np.random.rand(i,) 

# number of points in two spectra (two separate spectra of different length) 
# possibly obtained on different equipment such that the pixels/values don't align 
n1 = 450 
n2 = 950 
X1 = np.sort(f(n1)) 
X2 = np.sort(f(n2)) 
Y1 = f(n1) 
Y2 = f(n2) 

# make dataframe from the spectra 
d = dict() 
d['1'] = pd.Series(Y1, index=X1) 
d['2'] = pd.Series(Y2, index=X2) 
print(d) 
df = pd.DataFrame(d) 
print(df) 

這是一個不是,並返回我得到的錯誤:

import pandas as pd 
import numpy as np 

def f(i): 
    return np.random.rand(i,) 

# number of points in two spectra (two separate spectra of different length) 
# possibly obtained on different equipment such that the pixels/values don't align 
n1 = 450 
n2 = 950 
# store X array to duplicate a value within it 
X_ = f(n1) 
X1 = np.sort(np.append(X_,X_[0])) 
X2 = np.sort(f(n2)) 
Y_ = f(n1) 
Y1 = np.append(Y_,Y_[0]) 
Y2 = f(n2) 

# make dataframe from the spectra 
d = dict() 
d['1'] = pd.Series(Y1, index=X1) 
d['2'] = pd.Series(Y2, index=X2) 
print(d) 
df = pd.DataFrame(d) 
print(df)