简体   繁体   English

每天从两列或更多列熊猫中计算独特的出现次数

[英]Count unique occurrences per day from two or more columns pandas

I could like to count the unique occurrences of names per day from two columns:我想从两列中计算每天出现的名字的唯一次数:

df = pd.DataFrame({
    'ColA':['john wick','bloody mary','peter pan','jeff bridges','billy boy'],
    'ColB':['bloody mary','jeff bridges','billy boy','billy boy','john wick'],
    'date':['2000-01-01', '2000-01-01', '2000-01-03', '2000-01-03', '2000-01-03'],})
datetime_series = pd.to_datetime(df['date'])
datetime_index = pd.DatetimeIndex(datetime_series.values)
df2 = df.set_index(datetime_index)
df2.drop('date',axis=1,inplace=True)
df2
Out[746]: 
                    ColA          ColB
2000-01-01  john wick     bloody mary 
2000-01-01  bloody mary   jeff bridges
2000-01-03  peter pan     billy boy   
2000-01-03  jeff bridges  billy boy   
2000-01-03  billy boy     john wick   

So that I obtain a series or similar to the following:以便我获得一系列或类似于以下内容:

           unique occurrences of names
2000-01-01             3
2000-01-03             4

Use DataFrame.stack with DataFrameGroupBy.nunique and last Series.to_frame :DataFrame.stackDataFrameGroupBy.nunique和最后一个Series.to_frame

df3 = df2.stack().groupby(level=0).nunique().to_frame(name='unique occurrences of names')
print (df3)
            unique occurrences of names
2000-01-01                            3
2000-01-03                            4

Or alternative with DataFrame.melt :或者使用DataFrame.melt替代:

df3 = (df2.reset_index()
          .melt('index')
          .groupby('index')['value']
          .nunique()
          .to_frame(name='unique occurrences of names'))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM