0

I have a dataset given below:

import pandas as pd
data = pd.DataFrame({'A': ["A", "A", "A", "B", "B", "B", "A", "A", "A", "A", "B"],
                     'B': ["C", "D", "C", "C", "D", "C", "C", "D", "D", "C", "D"],
                     'C': ["A1", "A2", "A1", "A1", "A2", "A3", "A4", "A2", "A1", "A1", "A2"])

I need to have a function, func(List[str]) -> List[int], that takes the column names and returns me to get frequencies illustrated with given examples:

func(["A"]) -> [7, 4]
func(["A", "B"]) -> [4, 3, 2, 2]

Is there any way to solve this problem efficiently?

Henry Ecker
  • 34,399
  • 18
  • 41
  • 57
user3104352
  • 1,100
  • 1
  • 16
  • 34
  • 2
    `data.value_counts(['A']).to_list()` and `data.value_counts(['A', 'B']).to_list()` – LemonPy Sep 17 '21 at 01:40
  • This code snippet gives the error: AttributeError: 'DataFrame' object has no attribute 'value_counts' – user3104352 Sep 17 '21 at 01:43
  • DataFrame value_counts is only available in 1.1 and newer. – Henry Ecker Sep 17 '21 at 01:44
  • Before that need to use `size` or `count` see the differences in [What is the difference between size and count in pandas?](https://stackoverflow.com/q/33346591/15497888). `data.groupby('A').size().tolist()` or `data.groupby(['A', 'B']).size().tolist()` – Henry Ecker Sep 17 '21 at 01:45
  • Or `data.groupby(['A', 'B'])['C'].count().tolist()` or `data.groupby('A')['C'].count().tolist()` – Henry Ecker Sep 17 '21 at 01:46

0 Answers0