Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Difference between df[df['col a']] and df['col a']?

I’m new at coding and feel like to really understand it, I have to truly grasp the concepts.

Quality of life edit:

Why do we do df[df[‘col a’]] == x? INSTEAD of df[‘col a’] == x? when making a search? I understand that on the second expression I would be looking at column names that equal X but I’d love to know what does the addition of making it a list (df[]) does for the code

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I would love to know the difference between those two and what I am actually doing when I nest the column on a list.

any help is appreciated thank you so much!

>Solution :

So we use df[df['col a']== x] instead of just df['col a'] == x because to optimize the dataframe itself you are escencially telling the data frame with df['col a'] == x that you want a bool of true false if the condition is met (you can try this on your df and will see that when you do not put it in the df[] that it only will list df['col a'] == x as a list of true and false). so it pandas will first say "What asking"? then it will say "You asked for X here is a series of True/False based on what you asked" and finally "You asked for all of the only True values of the series here is the dataframe the reflects only true"

Does that help clear up what it is doing? Basically just pandas trying to be as optimal as possible. As well as when you start learning more and more you can add multiple arguments df[(df['col a'] == x) & (df['col b'] == y)] which would be hard to write and keep together if you only did df['col a'] for your serach

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading