How to Extract Columns With Partial String Matches in a DataFrame?

Barbara Streisand
Release: 2024-10-20 14:00:03
Original
837 people have browsed it

How to Extract Columns With Partial String Matches in a DataFrame?

Finding Columns with Partial String Matches

Querying a DataFrame for columns that contain a specific string can be a useful operation. However, what if the match is not exact but contains a certain substring? This is where the regex filter comes into play.

To locate columns with names containing a specific string, particularly a continuous substring, consider the following solution:

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

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

# Use regex filter to select columns with 'spike' substring
spike_cols = df.filter(regex='spike').columns.tolist()

# Print the column names with the matching substring
print(spike_cols)</code>
Copy after login

This code iterates over the DataFrame's columns using list comprehension and applies the regex filter to look for columns with the 'spike' substring. The resulting list of column names is then stored in the spike_cols variable, which can be used to access the corresponding columns as needed.

Another approach is to convert the column names to a list and iterate over them, testing each name for a substring match using a for loop and if statement:

<code class="python"># Column names converted to a list
col_list = list(df.columns)

# Iterate over the column names
for col in col_list:
    if 'spike' in col:
        # Column name with matching substring found
        print(col)</code>
Copy after login

By using these methods, you can efficiently identify and access columns in a DataFrame whose names contain a specific string, even if it is not an exact match.

The above is the detailed content of How to Extract Columns With Partial String Matches in a DataFrame?. 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!