Reputation: 1
I want to count the frequency of number values of csv data in my computer. I tried this code:
import pandas as pd
data = pd.read_csv("C:/address.csv")
df=pd.DataFrame(data==data)
df.apply(pd.value_counts)
I tried to use 'value_counts' but I don't know how to apply it.
Result that I want is in a row of "number(from 1 to 50): n times"
I hope that I want to solve this problem. Thank you.
Upvotes: 0
Views: 134
Reputation: 122
groupby together with count does not give the frequency of unique values.
Try using size, with group, to get unique values with their frequencies
Dataframe.groupby('column_name').size()
For faster computation and if your dataframe has values with same type, You can use;
index, counts = np.unique(df.values,return_counts=True)
If your values are all integers, then try using np.bincount()
Upvotes: 0
Reputation: 806
You can use the cumcount()
function.
df['number_count'] = df.groupby('col_of_interest').cumcount()
Upvotes: 0