Count Values in Pandas Dataframe

Count Values in Pandas Dataframe

Counting values in a pandas DataFrame can be approached in different ways depending on what you want to achieve. Here are several common operations:

1. Count Non-NA/null values for each column:

To count non-missing values for each column in a DataFrame, you can use the .count() method:

import pandas as pd df = pd.DataFrame({ 'A': [1, 2, 2, 3, 4, 5], 'B': [1, 1, 2, 3, 4, None], 'C': ['a', 'a', 'b', 'b', 'c', 'c'] }) print(df.count()) 

2. Count Unique Values in a Column:

To count distinct values in a column, you can use the .nunique() method:

print(df['A'].nunique()) # Outputs: 5 

3. Count Occurrences of Each Unique Value:

If you're interested in counting the occurrences of each unique value in a column, you can use the .value_counts() method:

print(df['A'].value_counts()) print(df['C'].value_counts()) 

4. Count Rows Based on a Condition:

If you want to count rows that meet a specific condition, you can use boolean indexing:

count_condition = df[df['A'] > 3].shape[0] print(count_condition) 

This will count the number of rows where the value in column A is greater than 3.

5. Count All Values in the DataFrame:

If you want to get the total number of individual values (cells) in the DataFrame:

total_values = df.size print(total_values) 

6. Count Rows and Columns:

To get the number of rows and columns in the DataFrame:

num_rows, num_cols = df.shape print("Number of Rows:", num_rows) print("Number of Columns:", num_cols) 

Remember, each of these methods serves different purposes, and the method you choose should be based on the specific counting task you want to perform.


More Tags

linestyle html-table pixel visible sparse-matrix eonasdan-datetimepicker comparator kerberos wordpress-rest-api conda

More Programming Guides

Other Guides

More Programming Examples