0

I have a csv file containing three columns. This link shows how my Dataframe is organized: https://github.com/phdvidal92/Files/blob/master/Data_Qnty.csv.

I would like to let just some records presented. For the SectionA, values equals to "Propose" and for the SectionB just "Prospecting".

I'm importing it with pandas read_csv

arqv2 = pd.read_csv('Data_Qnty.csv', skiprows = 2,
                    delimiter = ',', encoding = 'latin1')

After it, i'm trying to use drop

arqv2 = arqv2.drop(arqv2[(arqv2['SectionA'] != 'Prospecting') && (arqv2['SectionB'] != 'Propose')].index)

And it's not working. Do you know how can i achive my objective?

Thanks!

2
  • Text "it's not working" is the most useless information. Create short example data and expected result - and put it as text in question. Commented Apr 27, 2019 at 22:48
  • did you check arqv2.head() to see what you have ? If you skiprows=2 then you skip headers and your DF doesn't use names 'SectionA' 'SectionB'. Maybe you should read all and later drop rows to keep headers. Commented Apr 27, 2019 at 22:52

2 Answers 2

2

One method is to use query:

df.query('SectionA == "Propose" and SectionB =="Prospecting"')

Another method is to use boolean indexing:

df[(df['SectionA']  == 'Propose') & (df['SectionB'] == 'Prospecting')]
Sign up to request clarification or add additional context in comments.

Comments

0

If you want change the same dataframe, use inplace=True as second argument of drop(), and use the & operator, not &&, like this:

arqv2.drop(
    arqv2[(arqv2['SectionA'] != 'Prospecting') & 
          (arqv2['SectionB'] != 'Propose')].index, 
    inplace=True)

2 Comments

Thanks Hildeberto, do you know why we need to use inplace = True?
Was not working because of "&&". Use inplace=True for performance and better memory usage. It's don't make a copy of dataframe, and affect the underlying data. It's a parameter for various functions (replace, dropna, etc.). If you can not use inplace=True, use an assignment instead, to make a dataframe copy.

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.