Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.7k views
in Technique[技术] by (71.8m points)

python - Pandas Count Unique occurrences by Month

I have some monthly data that I'm trying to summarize using Pandas and I need to count the number of unique entries that occur each month. Here's some sample code that shows what I'm trying to do:

import pandas as pd

mnths = ['JAN','FEB','MAR','APR']
custs = ['A','B','C',]

testFrame = pd.DataFrame(index=custs, columns=mnths)
testFrame['JAN']['A'] = 'purchased Prod'
testFrame['JAN']['B'] = 'No Data'
testFrame['JAN']['C'] = 'Purchased Competitor'
testFrame['FEB']['A'] = 'purchased Prod'
testFrame['FEB']['B'] = 'purchased Prod'
testFrame['FEB']['C'] = 'purchased Prod'
testFrame['MAR']['A'] = 'No Data'
testFrame['MAR']['B'] = 'No Data'
testFrame['MAR']['C'] = 'Purchased Competitor'
testFrame['APR']['A'] = 'Purchased Competitor'
testFrame['APR']['B'] = 'purchased Prod'
testFrame['APR']['C'] = 'Purchased Competitor'

uniqueValues = pd.Series(testFrame.values.ravel()).unique()

#CODE TO GET COUNT OF ENTRIES IN testFrame BY UNIQUE VALUE

Desired Output:

                JAN    FEB    MAR    APR
purchased Prod   ?     ?       ?      ?
Purchased Competitor ? ?       ?      ?
No Data          ?     ?       ?      ?

I can get the unique values and create a new dataframe with the correct axes/columns

I started here and here: Pandas: Counting unique values in a dataframe Find unique values in a Pandas dataframe, irrespective of row or column location

but still can't quite get the output to the formats I need. I'm not quite sure how to apply the df.groupby syntax or the df.apply syntax to what I'm working with.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The filling is optional.

In [40]: testFrame.apply(Series.value_counts).fillna(0)
Out[40]: 
                      JAN  FEB  MAR  APR
No Data                 1    0    2    0
Purchased Competitor    1    0    1    2
purchased Prod          1    3    0    1

Here is a neat apply trick. I'll create a function and print out what is incoming (and maybe even debug in their). Then easy to see what's happening.

In [20]: def f(x):
   ....:     print(x)
   ....:     return x.value_counts()
   ....: 

In [21]: testFrame.apply(f)
A          purchased Prod
B                 No Data
C    Purchased Competitor
Name: JAN, dtype: object
A          purchased Prod
B                 No Data
C    Purchased Competitor
Name: JAN, dtype: object
A    purchased Prod
B    purchased Prod
C    purchased Prod
Name: FEB, dtype: object
A                 No Data
B                 No Data
C    Purchased Competitor
Name: MAR, dtype: object
A    Purchased Competitor
B          purchased Prod
C    Purchased Competitor
Name: APR, dtype: object
Out[21]: 
                      JAN  FEB  MAR  APR
No Data                 1  NaN    2  NaN
Purchased Competitor    1  NaN    1    2
purchased Prod          1    3  NaN    1

[3 rows x 4 columns]

So its doing this operation then concatting them together (with the correct labels)

In [22]: testFrame.iloc[0].value_counts()
Out[22]: 
purchased Prod          2
Purchased Competitor    1
No Data                 1
dtype: int64

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...