pandas 不明白:ValueError: Can only tuple-index with a MultiIndex

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/45721207/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 04:15:50  来源:igfitidea点击:

Don't understand: ValueError: Can only tuple-index with a MultiIndex

pythonpandas

提问by Demetrios Papakostas

I know this question already exists, but the answers havent helped me.

我知道这个问题已经存在,但答案并没有帮助我。

def function(T,theta,A):
   x=(T-theta)/A
   return(x)

filen=pd.read_csv('filename')
filelist=[file,file2,...,filen)
labels=['name1','name2',...]
colors=['red','blue','green',...]
for i in range(len(filelist)):
    x=filelist[i]['column1']
    y=filelist[i]['column2']
    y2=(1/y)
    plt.plot(x, y2, colors[i], label=labels[i])
    plt.legend()
    plt.plot()
    w=np.where(x>170)
    print(x[w])  #error, can only tuple index with multiindex
    other_fit=curve_fit(function,x,y)
    popt, pcov=other_fit
    plt.plot(x, function(x, *popt), colors2[i], label=labels2[i])
    plt.show()

The error occurs at x[w].
When I am not in the for loop, there is no error.

错误发生在x[w]
当我不在 for 循环中时,没有错误。

The error message is as follows:

ValueError: Can only tuple-index with a MultiIndex

错误信息如下:

ValueError: Can only tuple-index with a MultiIndex

回答by MaxU

Try to change:

尝试改变:

w=np.where(x>170)

to:

到:

w = x[x>170]

np.wherereturns a tuple in your case:

np.where在您的情况下返回一个元组:

Returns

If only `condition` is given, return the tuple
``condition.nonzero()``, the indices where `condition` is True.

退货

If only `condition` is given, return the tuple
``condition.nonzero()``, the indices where `condition` is True.