Certainly! In Python, you can use the pandas
library to handle data in DataFrames. Below are examples of how you can get the size and count of elements in a DataFrame using pandas
.
1. Getting the Size of a DataFrame (include NaN and None Values)
To get the size (i.e., the total number of elements) in a DataFrame, you can use the size
attribute.
2. Getting the Count of Non-NA/null Entries (Exclude NaN and None Values)
To get the count of non-NA/null entries for each column, you can use the count()
method.
import pandas as pd
# Sample DataFrame
data = {
'A': [1, 2, None],
'B': [4, None, 6],
'C': [None, 8, 9]
}
df = pd.DataFrame(data)
# Get the count of non-NA/null entries for each column
count_non_na = df.count()
print("Count of non-NA/null entries for each column:")
print(count_non_na)
print("
df.count(0)\n",df.count(0))print("
df.count(1)\n",df.count(1))
In pandas
, the count()
method is used to count the number of
non-NA/null entries in each column or row of a DataFrame.
This method is very useful for understanding how much data you
have in each part of your DataFrame, especially when dealing with
missing values.
- df.count(): Counts non-NA/null entries in each column.
- df.count(axis=1): Counts non-NA/null entries in each row.
- df.count(axis=0): Counts non-NA/null entries along columns (default).
Usage of the count()
Function
Here's how you can use the count()
method with a DataFrame:
1. Count Non-NA/null Entries in Each Column
By default, df.count()
counts the non-NA/null entries for each column.
import pandas as pd
# Sample DataFrame
data = {
'A': [1, 2, None],
'B': [4, None, 6],
'C': [None, 8, 9]
}
df = pd.DataFrame(data)
# Count of non-NA/null entries in each column
count_per_column = df.count()
print("Count of non-NA/null entries for each column:")
print(count_per_column)
Output:
Count of non-NA/null entries for each column:
A 2
B 2
C 2
dtype: int64
2. Count Non-NA/null Entries in Each Row
You can use the axis=1
parameter to count the non-NA/null
entries for each row.
pythonimport pandas as pd
# Sample DataFrame
data = {
'A': [1, 2, None],
'B': [4, None, 6],
'C': [None, 8, 9]
}
df = pd.DataFrame(data)
# Count of non-NA/null entries in each row
count_per_row = df.count(axis=1)
print("Count of non-NA/null entries for each row:")
print(count_per_row)
Output:
Count of non-NA/null entries for each row:
0 2
1 2
2 2
dtype: int64
3. Count Non-NA/null Entries for Specific Axis
You can use the axis
parameter to specify whether you want to count
entries along the rows or columns:
axis=0
(or omit axis
parameter): Counts along columns (default).axis=1
: Counts along rows.
Example of counting along columns (default behavior):
pythonimport pandas as pd
# Sample DataFrame
data = {
'A': [1, 2, None],
'B': [4, None, 6],
'C': [None, 8, 9]
}
df = pd.DataFrame(data)
# Count non-NA/null entries along columns
count_columns = df.count(axis=0)
print("Count of non-NA/null entries along columns:")
print(count_columns)
Example of counting along rows:
python
import pandas as pd
# Sample DataFrame
data = {
'A': [1, 2, None],
'B': [4, None, 6],
'C': [None, 8, 9]
}
df = pd.DataFrame(data)
# Count non-NA/null entries along rows
count_rows = df.count(axis=1)
print("Count of non-NA/null entries along rows:")
print(count_rows)
0 Comments
Please do note create link post in comment section