使用熊猫打印带有条件的特定列

数据挖掘 Python 数据集 熊猫
2021-09-16 08:58:10

我有一个包含 5 列的数据集,我只想在“CLASS”列等于 1 时打印名为“CONTENT”的列的内容。我知道 using.query 允许我选择一个条件,但它会打印整个数据集。我试图删除不需要的列,但我完成了未对齐且未完成的数据:-

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...  
4个回答

Pandas切片表示法中,必须首先指示要过滤的条件,最后才能选择要选择的列:特别是对于我们手头的示例:

df[df['CLASS']==1]['CONTENT'] 

假设您的数据框被称为df

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

print(df2['CONTENT'])

如原始帖子的评论和其他答案之一所示,您可以在要选择的列之前或之后指示过滤条件。确认此行为:

>>> 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

“条件后内容”的安排可能更容易阅读。

打印具有一个条件的多列的示例方法:

print(df[df["Total Profit"]>1000000][["Region","Country", "Item Type", "Total Profit"]])

打印具有多个条件的多列的示例方法:

print(df[df["Total Profit"]>1000000][df["Region"]=="Europe"][["Region","Country", "Item Type", "Total Profit"]])

上面的代码是示例,而不是给定问题的解决方案。