pandas 0.24.1 关键错误:“[Index(['A' 'B'], dtype='object')] 均不在 [columns] 中”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/54645074/
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
pandas 0.24.1 Key Error: "None of [Index(['A' 'B'], dtype='object')] are in the [columns]"
提问by Fatih1923
Formerly I had anaconda with pandas 0.18. Using the code below, I made a calculation by the function "calc_func" and assign the result to the the columns of DataFrame, say "A" and "B".
以前我有 anaconda 和 pandas 0.18。使用下面的代码,我通过函数“calc_func”进行计算并将结果分配给DataFrame的列,比如“A”和“B”。
df[["A", "B"]]=df.[["KV", "GV"]].apply(calc_func, axis=1)
After upgrading the pandas to 0.24.1, this code yields a KeyError as "None of [Index(['A' 'B'], dtype='object')] are in the [columns]"
将Pandas升级到 0.24.1 后,此代码会产生一个 KeyError,因为“[Index(['A' 'B'], dtype='object')] 不在 [columns] 中”
I have tried the code below but got the same error.
我已经尝试了下面的代码,但得到了同样的错误。
df.loc[:,["A", "B"]]=df.loc[:,["KV", "GV"]].apply(calc_func, axis=1)
Any help would be highly appreciated.
任何帮助将不胜感激。
My calc_func below:
我的 calc_func 如下:
def calc_func (_dataframe):
_limit15=v_limit15.get()
_limit20=v_limit20.get()
kvm=_veritabani["KV"]
gvm=_veritabani["GV"]
if kvm+gvm<=_limit15:
gvo=0.15
gv=gvm*gvo
elif kvm+gvm>_limit15 and kvm<=_limit15:
gv=(kvm+gvm-_limit15)*0.20+(_limit15-kvm)*0.15
gvo=gv/gvm
elif kvm>_limit15 and kvm+gvm<=_limit20:
gvo=0.20
gv=gvm*gvo
gvo=gv/gvm
elif kvm+gvm>_limit20 and kvm<=_limit20:
gv=(kvm+gvm-_limit20)*0.27+(_limit20-kvm)*0.20
gvo=gv/gvm
return [gvo, gv]
回答by jezrael
Change function for return Series
with specify index:
Series
使用指定索引更改返回函数:
return pd.Series([gvo, gv], index=['A','B'])