Python Forum

Full Version: Pandas: faster method to count occurrences
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi.
I'm a file myfile.csv like this:
Conc Date Run I II III IV V
1 13/10/2018 AA 4 23 28 79 11
2 16/10/2018 AA 41 1 43 30 9
3 18/10/2018 AA 21 12 13 56 5
4 20/10/2018 AA 51 25 1 43 14
5 23/10/2018 AA 44 64 47 14 50
6 25/10/2018 AA 90 47 1 33 82
7 27/10/2018 AA 64 63 43 4 33
8 30/10/2018 AA 4 23 3 58 33
9 02/11/2018 AA 61 48 40 41 11

My code is:
df=pd.read_csv("myfile.csv",encoding='windows-1252', sep="\t", decimal=',',keep_default_na=False, na_values=[''], converters={"Conc":str},)


df.set_index('Date',inplace=True)
df.set_index('Run',inplace=True)
df.set_index('Conc',inplace=True)

df=(df.apply(pd.value_counts).count(axis=1,numeric_only=True))
print("Occurrences number is:")
print(df)
Output is:
Number Occurrence
1 2
3 1
4 2
5 1
9 1
11 1
12 1
13 1
14 2
21 1
23 1
25 1
28 1
30 1
33 2
40 1
41 2
43 2
44 1
47 2
48 1
50 1
51 1
56 1
58 1
61 1
63 1
64 2
79 1
82 1
90 1

My question is:
1) Is there a faster method to count occurrences, instead of "df.apply(pd.value_counts).count"?

2) also, I ask:is there a method to arrange occurrences in descending order?

Thanks in advance