简体   繁体   English

KeyError: 0 尝试使用 DEF 时

[英]KeyError: 0 when trying to use a DEF

I'm new here, practicing python and I can't get this to work.我是新来的,正在练习 python ,但我无法让它工作。

I have a DF with 6 columns and multiple rows, all of them are dtype float64.我有一个 6 列和多行的 DF,它们都是 dtype float64。

I created a def so that it does this:我创建了一个def以便它执行此操作:

def completar_datos (var1,var2,var3,var4,var5,var6):
n = len(df5)
for i in range(n):
    if df5[var1][i]+df5[var2][i]+df5[var3][i] < 100 :
        df5[var4][i]= 100*df5[var1][i]/df5[var1][i]+df5[var2][i]+df5[var3][i]
        df5[var5][i]= 100*df5[var2][i]/df5[var1][i]+df5[var2][i]+df5[var3][i]
        df5[var6][i]= 100*df5[var3][i]/df5[var1][i]+df5[var2][i]+df5[var3][i]   

Basically, what I want is that for that loop, solve that operation and save it.基本上,我想要的是对于那个循环,解决那个操作并保存它。

But, I'm getting this error and don't know what to do:但是,我收到了这个错误,不知道该怎么做:

KeyError                                  Traceback (most recent call last)
<ipython-input-22-dfad1d30cb75> in <module>
----> 1 completar_datos('Health_exp_out_of_pocket_pct_2016','Health_exp_public_pct_2016','External_health_exp_pct_2016','Health_exp_Out_Pocket','Health_exp_Public','External_Health_exp')

<ipython-input-21-c5a069ca4dca> in completar_datos(var1, var2, var3, var4, var5, var6)
      2     n = len(df5)
      3     for i in range(n):
----> 4         if df5[var1][i]+df5[var2][i]+df5[var3][i] < 100 :
      5             df5[var4][i]= 100*df5[var1][i]/df5[var1][i]+df5[var2][i]+df5[var3][i]
      6             df5[var5][i]= 100*df5[var2][i]/df5[var1][i]+df5[var2][i]+df5[var3][i]

~\AppData\Local\Continuum\anaconda3\lib\site-packages\pandas\core\series.py in __getitem__(self, key)
    869         key = com.apply_if_callable(key, self)
    870         try:
--> 871             result = self.index.get_value(self, key)
    872 
    873             if not is_scalar(result):

~\AppData\Local\Continuum\anaconda3\lib\site-packages\pandas\core\indexes\base.py in get_value(self, series, key)
   4402         k = self._convert_scalar_indexer(k, kind="getitem")
   4403         try:
-> 4404             return self._engine.get_value(s, k, tz=getattr(series.dtype, "tz", None))
   4405         except KeyError as e1:
   4406             if len(self) > 0 and (self.holds_integer() or self.is_boolean()):

pandas\_libs\index.pyx in pandas._libs.index.IndexEngine.get_value()

pandas\_libs\index.pyx in pandas._libs.index.IndexEngine.get_value()

pandas\_libs\index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas\_libs\hashtable_class_helper.pxi in pandas._libs.hashtable.Int64HashTable.get_item()

pandas\_libs\hashtable_class_helper.pxi in pandas._libs.hashtable.Int64HashTable.get_item()

KeyError: 0

Any ideas on how to fix this?有想法该怎么解决这个吗? If you don't understand please ask me, this is my first post.如果你不明白请问我,这是我的第一篇文章。

THANKS!!谢谢!!

You don't want to loop over a data frame in this way.您不想以这种方式循环数据框。 Define a function and apply it to a column or the entire data frame.定义 function 并将其应用于列或整个数据框。 Look at the pandas documentation for apply for details.查看 pandas 文档了解apply详情。

The source of your error seems to be that pandas is looking for a column with the name 0, and that name doesn't exist, so it throws a KeyError.您的错误的来源似乎是 pandas 正在寻找名称为 0 的列,而该名称不存在,因此会引发 KeyError。 You are trying to use array subscripts on a data frame.您正在尝试在数据框上使用数组下标。 If you want to access rows and columns of a data frame, use df.loc or df.iloc .如果要访问数据框的行和列,请使用df.locdf.iloc

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM