2017-03-09 21 views
2

私はこれを持ってpandas-最初の10行に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 

作業CSVとして、私は対象列を取り、ストリングとして「ABA」を持っているすべての行のためにカウントする必要があります。 'DataFrameGroupBy' オブジェクトの呼び出し可能な属性 'to_stringに' にアクセスできません:はAttributeError -

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

エラーを取得する - ここでは、この

aba 12 
3aba 5 
INVaba 2 

よう

Occurrences of aba- 512 

かさえも結果は私のコードです'apply'メソッドを試してみてください

*****注:これは以前の

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

を、私を与える - - 異なるファイルタイプのUNT、これは動作します

Delivered in Email 
Attachment 32647 
Header   131 
URL   9236 

答えて

2

フルカウントを取得するには、str.containsとそれに続くcountを使用してください。そして、'aba'が含まれているユニークな文字列のためのカウントを取得する

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

、あなたは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 
を与えます
関連する問題