For examples, I get a dataframe as follows:
df
col1 col2 col3
row1 1 2 3
row2 1 0 0
row3 1 0 0
do_value_counts(df)
value_count
0 4
1 3
2 1
3 1
I try to reshape dataframe into (n,1) but it is not easy for Pandas, so I try to initialize a numpy array with it and reshape it into (n, 1).
Then I use np.bincount()
to do the things like pd.value_counts()
.
BUT I get (31280, 1) shape numpy array and object too deep for desired array
when I try to use np.bincount
on it.
So I am wondering if there is a better way to implement it?
If not mind could anyone help me?
Thanks in advances.
try to melt the dataframe before:
df.melt().value.value_counts()
# 0 4
# 1 3
# 2 1
# 3 1
a numpy way would be :
unique, counts = np.unique(df.values.ravel(), return_counts=True)
counts_df = pd.DataFrame({"counts":counts}, index=unique)
# counts
# 0 4
# 1 3
# 2 1
# 3 1
User this, maybe it will solve what you need.
item, count = np.unique(np.array(df).flatten(), return_counts=True)
pd.DataFrame(count, index=item, columns=['value_counts'])
value_counts
0 4
1 3
2 1
3 1
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.