2017-03-09 123 views
2

我曾以此为CSV在pandas-前十行Python的大熊猫筛选和GROUPBY

print frame1.head(10) 

     alert   Subject filetype type  country status 
0 33965790 44676 aba  Attachment doc RU,RU,RU,RU deleted 
1 33965786 44676 rcrump Attachment zip   NaN deleted 
2 33965771   3aba Attachment zip   NaN deleted 
3 33965770    NaN Attachment js   ,, deleted 
4 33965766    NaN Attachment js   ,, deleted 
5 33965761    NaN Attachment zip   NaN deleted 
6 33965760    NaN Attachment zip   NaN deleted 
7 33965757    NaN Attachment zip   NaN deleted 
8 33965751 35200  3aba Attachment doc  RU,RU,RU deleted 
9 33965747 35200 INVaba Attachment zip   NaN deleted 

我需要拍摄的对象列数和计数具有“ABA”作为一个子字符串的所有行工作。

Occurrences of aba- 512 

,甚至导致这样

aba 12 
3aba 5 
INVaba 2 

这里是我的代码 -

targeted = frame1[frame1['Subject'].str.contains('aba', case=False , na=False)].groupby('Subject') 
print (targeted.to_string(header=False)) 

得到的错误 - AttributeError错误:无法访问 'DataFrameGroupBy' 对象的可调用属性 'to_string',尝试使用“应用”方法

*****注:我得到这个工作更早一个公司UNT不同的文件类型,这个工程 -

filetype = frame1.groupby('filetype').size() 
###clean up the printing 
print "Delivered in Email" 
print (filetype.to_string(header=False)) 

,并给了我 -

Delivered in Email 
Attachment 32647 
Header   131 
URL   9236 

回答

2

要得到一个完整的计数,只需使用str.contains然后count

>>> df.Subject.str.contains('aba', case=False, na=False).count() 
10 

然后以获取包含'aba',您可以访问由contains发现这些值,然后使用value_counts唯一字符串计数。

>>> df.loc[df.Subject.str.contains('aba', case=False, na=False), 'Subject'].value_counts() 

3aba  1 
INVaba 1 
aba  1 
Name: Subject, dtype: int64 
0

对你的建议,你可以这样做下面的第一输出:

containts_aba = frame1[frame1['Subject'].str.contains('aba', case=False) 
print("Occurrences of aba-",len(contains_aba)) 

它创建另一个数据框基于您的条件,然后该数据框的长度将是出现次数,然后您可以将其打印出来。

0
targeted = frame1[frame1['Subject'].str.contains('aba', case=False , na=False)].groupby('Subject').size() 
print (targeted.to_string(header=False)) 

给人

3aba  1 
INVaba 1 
aba  1