python - Grouping the values of all columns by index of a pandas dataframe -
i want build distribution of total no. of videos user has watched. watch signified 1 else 0. users index of data frame.
assume data this:
a b c user1 1 1 0 user2 0 1 0 user3 1 0 1
i want each use count of 1 in row.
i doing doesn't seem work. dont want use applymap function seem slow.
d.groupby(d.index).sum(axis=1)
gives error axis not recognized
if have duplicates in index, can use groupby
double sum
:
print (df) b c user1 1 1 0 user1 1 1 1 user2 0 1 0 user3 1 0 1 print (df.groupby(df.index).sum().sum(1)) user1 5 user2 1 user3 2 dtype: int64
if there no duplicates, use sum
- psidom comment:
df.sum(axis=1)
edit:
import matplotlib.pyplot plt df.sum(axis=1).plot.hist() plt.show()
Comments
Post a Comment