Nachengue
Nachengue

Reputation: 385

Error using a dataframe created by a boolean indexing

EDIT

I had this dataframe:

df = pd.DataFrame({'a':['nonono','sisisis','some_other_string', 'blebleble', 
                        'jajajaja', 'ohyeah','some_string','blablablananana'], 
                   'b':[0,1,2,3,4,5,6,7]})

I had make a boolean indexing to create a new dataframe as the docs says.

df2 = df.loc[df.a != 'some_string', :]

To check the difference in the length of the dataframe I make print(len(df) > len(df2)) and the ouput is True.That means that the boolean indexing gone right, doesn't it?

But then, it raise KeyError: 6 when I'm traing to print True or False based on some conditions. This is the code:

for i in list(range(len(df2.a))):
  if 'blabla' in df2.a[i]:
    print(True)
  else:
    print(False)

The output is

False
False
False
False
False
False
---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-85-2c4113790af5> in <module>()
      1 for i in list(range(len(facturacion.formulario))):
----> 2   if 'NOTA DE CREDITO' in facturacion.formulario[i]:
      3     print(True)
      4   else:
      5     print(False)

1 frames
/usr/local/lib/python3.6/dist-packages/pandas/core/indexes/base.py in get_value(self, series, key)
   4403         k = self._convert_scalar_indexer(k, kind="getitem")
   4404         try:
-> 4405             return self._engine.get_value(s, k, tz=getattr(series.dtype, "tz", None))
   4406         except KeyError as e1:
   4407             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: 6

If I make

for i in list(range(len(df2.a[:6]))):
  if 'blabla' in df2.a[i]:
    print(True)
  else:
    print(False)

The output is

False
False
False
False
False
False

I can't realized what's happening. Any suggest?

Upvotes: 0

Views: 912

Answers (1)

cristian hantig
cristian hantig

Reputation: 1310

I think this is the problem:

df2.a[i]

Your 6th index was deleted while indexing

df2 = df.loc[df.a != 'some_string', :]

You should reset the indices:

df2.reset_index(drop=True)

Or You should use loc:

df2.a.loc[i]

Upvotes: 3

Related Questions