How to Extract Columns with Matching Substrings in pandas DataFrame Iteratively and Using Regular Expressions?

Susan Sarandon
Release: 2024-10-20 13:58:29
Original
493 people have browsed it

How to Extract Columns with Matching Substrings in pandas DataFrame Iteratively and Using Regular Expressions?

Identifying Columns Containing Specific Substrings

To locate columns whose names contain a specified substring without requiring an exact match, an iterative approach can be employed. This involves examining each column name and identifying those that satisfy the search criterion.

Consider a DataFrame with column names such as 'spike-2', 'hey spike', and 'spiked-in'. To extract the column names containing the substring 'spike', the following Python code can be utilized:

<code class="python">import pandas as pd

# Initialize data
data = {'spike-2': [1,2,3], 'hey spke': [4,5,6], 'spiked-in': [7,8,9], 'no': [10,11,12]}
df = pd.DataFrame(data)

# Iterate over column names and filter based on substring
spike_cols = [col for col in df.columns if 'spike' in col]

# Print resulting column names
print(spike_cols)</code>
Copy after login

In this code:

  1. df.columns returns a list of column names.
  2. The list comprehension [col for col in df.columns if 'spike' in col] iterates over each column name using the variable col and constructs a new list containing only names that include the substring 'spike'.
  3. The resulting spike_cols contains the desired column names, which can be accessed later using df['col_name'] or df[col_name].

Alternatively, to obtain a DataFrame with only the matching columns:

<code class="python">df2 = df.filter(regex='spike')</code>
Copy after login

This will create df2 containing only the columns whose names include 'spike'.

The above is the detailed content of How to Extract Columns with Matching Substrings in pandas DataFrame Iteratively and Using Regular Expressions?. For more information, please follow other related articles on the PHP Chinese website!

source:php
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!