3
$\begingroup$

I have a data set which contains 5 columns, I want to print the content of a column called 'CONTENT' only when the column 'CLASS' equals one. I know that using .query allows me to select a condition, but it prints the whole data set. I tried to drop the unwanted columns, but I finished up with unaligned and not completed data: -

CONTENT CLASS
0 Huh, anyway check out this you[tube] channel: ... 1  
1 Hey guys check out my new channel and our firs... 1  
2 just for test I have to say murdev.com 1  
3 me shaking my sexy ass on my channel enjoy ^^ \xef\xbb\xbf 1  
4 watch?v=vtaRGgvGtWQ Check this out .\xef\xbb\xbf 1  
5 Hey, check out my new website!! This site is a... 1  
6 Subscribe to my channel \xef\xbb\xbf 1  
8 You should check my channel for Funny VIDEOS!!\xef\xbb\xbf 1  
9 and u should.d check my channel and tell me wh... 1  
10 Hey subscribe to me\xef\xbb\xbf 1  
11 Once you have started reading do not stop. If... 1  
12 https://twitter.com/GBphotographyGB\xef\xbb\xbf 1  
13 subscribe like comment\xef\xbb\xbf 1  
14 please like :D https://premium.easypromosapp.c... 1  
15 Hello! Do you like gaming, art videos, scienti... 1  
17 http://www.ebay.com/itm/171183229277?ssPageNam... 1  
18 http://ubuntuone.com/40beUutVu2ZKxK4uTgPZ8K\xef\xbb\xbf 1  
19 We are an EDM apparel company dedicated to bri... 1  
21 subscribe to my channel people :D\xef\xbb\xbf 1  
22 Show your AUBURN PRIDE HERE: http://www.teespr... 1  
24 CHECK OUT MY CHANNEL 1  
25 marketglory . com/strategygame/andrijamatf ear... 1  
26 Hey guys! Im a 12 yr old music producer. I mak... 1  
27 Check me out! I\'m kyle. I rap so yeah \xef\xbb\xbf 1  
29 Subscribe to me for free Android games, apps.. \xef\xbb\xbf 1  
30 everyone please come check our newest song in ... 1  
32 sub my channel for no reason --\xef\xbb\xbf 1  
33 Check out my dubstep song "Fireball", made wit... 1  
36 Check my channel please! And listen to the bes... 1  
37 SUB 4 SUB PLEASE LIKE THIS COMMENT I WANT A SU... 1  
.. ... ...  
297 Please help me go to college guys! Thanks from... 1  
298 https://www.facebook.com/SchoolGeniusNITS/phot... 1  
299 I am so awesome and smart!!! Sucscribe to me!\xef\xbb\xbf 1  
300 Follow 4 Follow @ Va... 1  
301 http://hackfbaccountlive.com/?ref=4436607 psy... 1  
302 https://www.facebook.com/nicushorbboy add mee ... 1  
303 im sorry for the spam but My name is Jenny. I ... 1  
305 please throw a sub on my channel\xef\xbb\xbf 1  
307 Go to my channel if u want to see a fly gettin... 1  
309 COME AND CHECK OUT MY NEW YOUTUBE CHHANEL, GOI... 1  
311 Please check out my vidios guys\xef\xbb\xbf 1  
313 Hey guys can you check my channel out plz. I d... 1  
315 PLEASE SUBSCRIBE ME!!!!!!!!!!!!!!!!!!!!!!!!!!!... 1  
317 Check out my Music Videos! Fuego - U LA LA Rem... 1  
318 Check out my Music Videos! and PLEASE SUBSCRIB... 1  
319 www.marketglory.com/strategygame/lordviperas\xef\xbb\xbf 1  
323 Limit sun exposure while driving. Eliminate th... 1  
325 http://hackfbaccountlive.com/?ref=5242575\xef\xbb\xbf 1  
327 https://www.facebook.com/FUDAIRYQUEEN?pnref=st... 1  
329 FOLLOW MY COMPANY ON TWITTER thanks. https:/... 1  
331 Hey come check us out were new on youtube let ... 1  
333 Look at the pictures, if not difficult http://... 1  
335 Hey guys can you check my YouTube channel I kn... 1  
337 https://www.facebook.com/tofikmiedzynB/photos/... 1  
338 https://www.facebook.com/eeccon/posts/73394924... 1  
339 http://www.bing.com/explore/rewards?PUBL=REFER... 1  
340 Please do buy these new Christmas shirts! You ... 1  
341 Free my apps get 1m crdits ! Just click on the... 1  
347 subscribe to me for call of duty vids and give... 1  
348 hi guys please my android photo editor downloa...  
$\endgroup$
  • 2
    $\begingroup$ df['CONTENT'][df['CLASS'] == 1]. $\endgroup$ – Ankit Seth Jul 16 '18 at 14:25
  • $\begingroup$ I think this already has answer on stackoverflow. Also, what's wrong with the data you got? It is showing all 348 rows. $\endgroup$ – Ankit Seth Jul 16 '18 at 14:28
  • $\begingroup$ @AnkitSeth This worked for me, but the output is like the one I got, the content is not completed (long content end with '...'). Is there anyway to show the whole content? $\endgroup$ – Mohamed Abduljawad Jul 16 '18 at 14:38
  • 1
    $\begingroup$ Which editor are you using? If you want to print the whole content, use a for loop and print the lines which you want to see. By default, it will not show this much long strings. $\endgroup$ – Ankit Seth Jul 16 '18 at 15:01
  • 1
    $\begingroup$ Thanks to @AnkitSeth I used what he suggested: df['CONTENT'][df['CLASS'] == 1] $\endgroup$ – Mohamed Abduljawad Jul 16 '18 at 15:20
5
$\begingroup$

In Pandas slice notation one must first indicate the condition to filter on and only eventually the column to select: in particular for the example at hand we have:

df[df['CLASS']==1]['CONTENT'] 
$\endgroup$
1
$\begingroup$

Assuming your data frame is called df:

df2 = df[df['CLASS'] == 1]

print(df2['CONTENT'])

$\endgroup$
  • $\begingroup$ it didn't work, I get this error,KeyError: False $\endgroup$ – Mohamed Abduljawad Jul 16 '18 at 14:28
  • $\begingroup$ The equal sign must be outside the square brackets for this to work. $\endgroup$ – gented Jul 16 '18 at 16:15
  • $\begingroup$ @gented you're right - amended the typo. $\endgroup$ – bradS Jul 17 '18 at 7:41
1
$\begingroup$

As shown in the comments on the original post and in one of the other answers, you can indicate the filter condition either BEFORE or AFTER the column to select. Confirming this behavior:

>>> import pandas
>>> df = pandas.DataFrame({'CLASS': [0,1], 'CONTENT': [1,6]})
>>> df
  CLASS  CONTENT
  0      0        1
  1      1        6
>>> print df['CONTENT'][df['CLASS'] == 1]
1    6

>>> print df[df['CLASS'] == 1]['CONTENT']
1    6

The "conditional-after-content" arrangement is perhaps a bit easier to read.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.