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 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…