Skip to content

Tarik Billa

  • Web Development
    • html
    • vue.js
    • laravel
    • css
    • javascript
    • jquery
    • node.js
    • php
    • asp.net
  • Programming
    • python
    • java
    • c
    • c++
    • c#
  • git
  • android

frequency

Relative frequencies / proportions with dplyr

October 13, 2022 by Tarik

Try this: mtcars %>% group_by(am, gear) %>% summarise(n = n()) %>% mutate(freq = n / sum(n)) # am gear n freq # 1 0 3 15 0.7894737 # 2 0 4 4 0.2105263 # 3 1 4 8 0.6153846 # 4 1 5 5 0.3846154 From the dplyr vignette: When you group by multiple variables, … Read more

Categories r Tags dplyr, frequency, group-by, r Leave a comment

How to count the frequency of the elements in an unordered list? [duplicate]

October 2, 2022 by Tarik

In Python 2.7 (or newer), you can use collections.Counter: >>> import collections >>> a = [5, 1, 2, 2, 4, 3, 1, 2, 3, 1, 1, 5, 2] >>> counter = collections.Counter(a) >>> counter Counter({1: 4, 2: 4, 5: 2, 3: 2, 4: 1}) >>> counter.values() dict_values([2, 4, 4, 1, 2]) >>> counter.keys() dict_keys([5, 1, … Read more

Categories python Tags frequency, list, python Leave a comment

Count the frequency that a value occurs in a dataframe column

September 17, 2022 by Tarik

Use value_counts() as @DSM commented. In [37]: df = pd.DataFrame({‘a’:list(‘abssbab’)}) df[‘a’].value_counts() Out[37]: b 3 a 2 s 2 dtype: int64 Also groupby and count. Many ways to skin a cat here. In [38]: df.groupby(‘a’).count() Out[38]: a a a 2 b 3 s 2 [3 rows x 1 columns] See the online docs. If you wanted … Read more

Categories python Tags frequency, pandas, python Leave a comment
Newer posts
← Previous Page1 Page2 Page3

Tarik Billa

Software Engineer
tarikbilla@gmail.com
+8801884414000
  • Reuse a hash in YAMLApril 17, 2024
  • Dockerfile: how to redirect the output of a RUN command to a variable?April 16, 2024
  • How to cd to a directory with spaces in the directory name?April 16, 2024
  • Maximum MIME type length when storing the type in a databaseApril 16, 2024
  • What is the difference between Unit, Integration, Regression and Acceptance Testing?April 16, 2024
© 2026 Tarik Billa