I'm new here, practicing python and I can't get this to work.
I have a DF with 6 columns and multiple rows, all of them are dtype float64.
I created a def so that it does this:
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. Look at the pandas documentation for apply
for details.
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. 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
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.