how to count how many times a string occurs in a column using pandas

krish

MY DF:

c1 | C2 | C3
A  | B  | C
A  | B  | N
S  | B  | I

I want to know how many times B occured in column C2.

I want that output in a list

Desired output:

mylist=[3]
EFT

One approach, which could generalize well if you later want to know how many of two or more different values appear in a field, is to use value_counts:

df['C2'].value_counts()
Out[28]: 
B     3
Name: C2, dtype: int64

df['C2'].value_counts().tolist()
Out[29]: [3]

df['C2'].value_counts().to_dict()
Out[30]: {'B ': 3}

df['c1'].value_counts()
Out[31]: 
A     2
S     1
Name: c1, dtype: int64

df['c1'].value_counts().tolist()
Out[32]: [2, 1]

df['c1'].value_counts().to_dict()
Out[33]: {'A ': 2, 'S ': 1}

Edit:

To get value_counts list output ordered based on first appearance, you could use

df['c1'].value_counts().reindex(df['c1'].unique()).tolist()

Ex:

df
Out[65]: 
  c1  C2 C3
0  S  B   C
1  A  B   N
2  A  B   I

df['c1'].value_counts().reindex(df['c1'].unique()).tolist()
Out[66]: [1, 2]

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to count how many times a string occurs

From Dev

How to count how many times a number occurs in each column of a file?

From Dev

Numpy, How to count how many times a string occurs

From Dev

How to count the number of times a value occurs in each column (per column), using Pandas?

From Javascript

JavaScript: How many times a character occurs in a string?

From Dev

Pandas Python Count how many times a column contains a list of numbers

From Dev

Count how many times a column contains a certain value in Pandas

From Dev

Count how many times a value occurs in a range (based on the value in another column)

From Dev

Count how many times an object occurs in a list of a list within a DataFrame column

From Dev

Pandas: Is there a way to count how many times a certain valued row occurs after another set of valued rows

From Dev

Count how many times a value occurs in a pandas data frame based on a condition

From Dev

XQuery: How to count how many times a value occurs in sequence

From Dev

Count how many times values has changed in column using R

From Dev

Count how many times a special character (non-spacing mark) occurs in a string in Swift

From Dev

How can i check that how many times each element of splitted string occurs in each column?

From Dev

count how many times each item occurs in a dictionary

From Dev

Count how many times a time occurs within a date range

From Dev

Count how many times a value occurs across tables

From Dev

Count how many times specific element occurs in array

From Dev

Count how many times certain text combinations occurs in certain columns

From Dev

Python: Count how many times a word occurs in a file

From Dev

Count how many times a word occurs in a HashMap per key

From Dev

How to check how many times a substring occurs within a string?

From Dev

Count how many times a word exist in column

From Dev

Count how many times a character changes in a string

From Dev

Make a function that shows how many times a word occurs in a string/sentence

From Java

Determining how many times a substring occurs in a string in Python

From Java

How do I count the number of times a sequence occurs in a Java string?

From Dev

Count how many times values appear in each alternate instance in a column in pandas dataframe

Related Related

  1. 1

    How to count how many times a string occurs

  2. 2

    How to count how many times a number occurs in each column of a file?

  3. 3

    Numpy, How to count how many times a string occurs

  4. 4

    How to count the number of times a value occurs in each column (per column), using Pandas?

  5. 5

    JavaScript: How many times a character occurs in a string?

  6. 6

    Pandas Python Count how many times a column contains a list of numbers

  7. 7

    Count how many times a column contains a certain value in Pandas

  8. 8

    Count how many times a value occurs in a range (based on the value in another column)

  9. 9

    Count how many times an object occurs in a list of a list within a DataFrame column

  10. 10

    Pandas: Is there a way to count how many times a certain valued row occurs after another set of valued rows

  11. 11

    Count how many times a value occurs in a pandas data frame based on a condition

  12. 12

    XQuery: How to count how many times a value occurs in sequence

  13. 13

    Count how many times values has changed in column using R

  14. 14

    Count how many times a special character (non-spacing mark) occurs in a string in Swift

  15. 15

    How can i check that how many times each element of splitted string occurs in each column?

  16. 16

    count how many times each item occurs in a dictionary

  17. 17

    Count how many times a time occurs within a date range

  18. 18

    Count how many times a value occurs across tables

  19. 19

    Count how many times specific element occurs in array

  20. 20

    Count how many times certain text combinations occurs in certain columns

  21. 21

    Python: Count how many times a word occurs in a file

  22. 22

    Count how many times a word occurs in a HashMap per key

  23. 23

    How to check how many times a substring occurs within a string?

  24. 24

    Count how many times a word exist in column

  25. 25

    Count how many times a character changes in a string

  26. 26

    Make a function that shows how many times a word occurs in a string/sentence

  27. 27

    Determining how many times a substring occurs in a string in Python

  28. 28

    How do I count the number of times a sequence occurs in a Java string?

  29. 29

    Count how many times values appear in each alternate instance in a column in pandas dataframe

HotTag

Archive