How can I count number of columns whose name starts with specific words?
Counting the number of columns in a DataFrame whose names start with specific words is a common task for data analysis and manipulation. In Python, you can use the str.startswith()
method and the sum()
function to accomplish this task.
Get the First and Last Column Names
To return an array containing the names of the first and last columns that start with a specific word, you can use the following code:
df.columns[df.columns.str.startswith('Name_')][[0,-1]]
In this code:
df.columns
represents the collection of column names in the DataFrame.df.columns.str.startswith('Name_')
filters the column names to include only those that start with'Name_'
.[[0,-1]]
extracts the first and last elements from the filtered column names.
Count the Number of Columns
To count the number of columns that start with a specific word, you can use the following code:
df.columns.str.startswith('Name_').sum()
In this code:
df.columns.str.startswith('Name_')
filters the column names to include only those that start with'Name_'
..sum()
calculates the sum of the filtered column names, which is equivalent to counting the number of columns that satisfy the condition.
Example
Consider the following DataFrame:
Name_1 Name_2 Age City
0 John Jane 20 New York
1 Mark Emily 25 Los Angeles
2 Tom Lily 30 San Francisco
To get the first and last column names that start with 'Name_'
, you can use the following code:
df.columns[df.columns.str.startswith('Name_')][[0,-1]]
The output of this code will be:
Index(['Name_1', 'Name_2'], dtype='object')
To count the number of columns that start with 'Name_'
, you can use the following code:
df.columns.str.startswith('Name_').sum()
The output of this code will be:
2
Conclusion
Using the str.startswith()
method and the sum()
function, you can easily count the number of columns in a DataFrame whose names start with specific words. This technique is useful for various data analysis and manipulation tasks.