1

I have a Dataframe named df which looks like -

pageno     entity          code         rawentity 
17727425   SAUDI           CBCNTRY      saudi 
17727425   GARRA           DRWRNAME     garra
17727425   PO BOX          RBCNTRY      po box 
17727425   NEW ZEALAND     DRWRCNTRY    new zealand

I also have a country list containing names of countries.It is of type 'list'.

I'm trying to keep only those value whose code is either - CBCNTRY or RBCNTRY or DRWRCNTRY and the entity should be in the countrylist.

The code that I have written is -

        for row in df.itertuples():

            if(row.code in ['DRWRCNTRY', 'RBCNTRY', 'CBCNTRY']):
                if(row.entity not in countrylist):
                    df.drop((row.index), inplace=True)

But I'm getting the following error -

Error is: labels [<built-in method index of Pandas object at 0x0000020A1BCE4EB8>] not contained in axis

I just want to know why my approach is wrong and is there any thing better I can do apart from this method.

I have searched this error but I'm unable to get a satisfactory answer.

2
  • What is countrylist here? Commented Jul 9, 2018 at 14:52
  • 1
    You are removing rows as you iterate over your dataframe. You should never do this. See @chuni0r's answer. Commented Jul 9, 2018 at 16:02

3 Answers 3

1

As suggested by Gerardo, use pd.isin and combine your expressions using boolean operators:

countrylist = ['SAUDI']
codelist = ['DRWRCNTRY', 'RBCNTRY', 'CBCNTRY']
df = df[(df['code'].isin(codelist)) & (df['entity'].isin(countrylist))]

results in

     pageno entity     code rawentity
0  17727425  SAUDI  CBCNTRY     saudi
Sign up to request clarification or add additional context in comments.

Comments

1

When you're working with series you can use pd.isin. For example, what you want can be achieved by doing:

df = df[df['code'].isin(['DRWRCNTRY', 'RBCNTRY', 'CBCNTRY'])]

Comments

1

You can use np.where and isin

suppose you have list called countrylist that contains SAUDI

countrylist = ['SAUDI']
df['code'] = np.where((df['code'] == 'CBCNTRY')| (df['code'] == 'RBCNTRY') | (df['code'] == 'DRWRCNTRY'),
                      df['code'], np.nan
                      )

df['code'] = np.where(df['entity'].isin(countrylist), df['code'], np.nan)

df.dropna(how='any', inplace= True)

print(df)

  pageno entity     code rawentity
17727425  SAUDI  CBCNTRY     saudi

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.