Python Forum
Pandas: faster method to count occurrences
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Pandas: faster method to count occurrences
#1
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Arrays faster than pandas? Mark17 4 2,801 Aug-02-2021, 03:14 PM
Last Post: Mark17
  Remove extra count columns created by pandas groupby spyf8 1 2,687 Feb-10-2021, 09:19 AM
Last Post: Naheed
  Pandas Import CSV count between numerical values within 1 Column ptaylor520 3 2,606 Jul-16-2019, 08:13 AM
Last Post: ptaylor520
  Finding date count from a list of date range in pandas trillerducas72 0 2,720 May-24-2018, 02:30 AM
Last Post: trillerducas72

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020