What are some effective ways to filter out specific string patterns while using Pandas?

dataframe

df.columns=['ipo_date','l2y_gg_date','l1k_kk_date']

Purpose

  • extract dataframe with columns titled _date excluding ipo_date.

Solution

  • df.filter(regex='_date&^ipo_date')

Answer №1

Take advantage of a negative lookbehind pattern:

import numpy as np
import pandas as pd

df = pd.DataFrame(np.arange(1, 21).reshape((5, 4)),
                  columns=['ipo_date', 'l2y_gg_date', 'l1k_kk_date', 'other'])

filtered = df.filter(regex=r'(?<!ipo)_date')
print(filtered)

Here is a sample df:

   ipo_date  l2y_gg_date  l1k_kk_date  other
0         1            2            3      4
1         5            6            7      8
2         9           10           11     12
3        13           14           15     16
4        17           18           19     20

The resulting filtered dataframe:

   l2y_gg_date  l1k_kk_date
0            2            3
1            6            7
2           10           11
3           14           15
4           18           19

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

I am encountering a TypeError because the type being used is unhashable, specifically a 'list'. How do I go about inserting an image into a list while preserving

Within my views.py file, I have the following code: import os import cv2 from pathlib import Path path1 = Path(__file__).parent / "../test1" path2 = Path(__file__).parent / "../test2" index_list = [] for i in path1.iterdir(): i = str(i) if i.split(" ...

What is the reason behind numpy.angle() not being a universal function (ufunc)?

What makes numpy.angle() different from other numpy universal functions (ufuncs)? Although it seems to meet the criteria outlined in the numpy documentation, why is it not officially categorized as a ufunc? I initially speculated that its conversion of c ...

Incapable of making a comparison between input variable and data stored in a .txt file / Unable to access file for data comparison

Currently, I am working on a program that allows users to both log in and register with their username and password stored in a separate .txt file. While registering is functioning correctly and writing data without any problems, I am encountering difficul ...

Python drawing techniques: A step-by-step guide

I am trying to divide a triangle into two colors, with the left side in red and the right side in yellow. But despite my efforts, I can't seem to achieve this split. When you run my code, you will see two triangles instead of one divided triangle as d ...

Halting the execution of Python processes initiated by a subprocess through user input without terminating the subprocess

Currently, I am in the process of developing a Python shell project that requires the capability to pause and background a running subprocess. Unfortunately, I have encountered difficulties with existing methods for pausing the subprocess, as they seem to ...

Tips for implementing an IF statement within a FOR loop to iterate through a DataFrame efficiently in Python

I am currently working on a task that involves selecting segments or clauses of sentences based on specific word pairs that these segments should start with. For instance, I'm only interested in sentence segments that begin with phrases like "what doe ...

"Getting overwhelmed with unpacking values" occurs when working with a list

def calculate_shortest_distance(list1, list2): print(list1) print(list2) shortest = sys.float_info.max distance = 0.0 for x1,y1 in list1: for x2,y2 in list2: distance = math.sqrt((float(x1)-float(x2))**2.0 + (float(y ...

Execute a Python script on multiple files located in various directories

I'm dealing with a Python script that utilizes two separate Excel files for data processing. These files are referenced in the same manner within the script but are stored in different folders. The script specifies from which folders to retrieve the f ...

Is it possible to convert a SearchQuerySet into a QuerySet without altering the existing order?

I possess a variable named doctors which is an object of type SearchQuerySet and my intention is to transform it into a QuerySet: doctors = SearchQuerySet().dwithin('location', point_data, max_dist).distance('location',point_data).orde ...

Finding the value within a div element with Selenium and Python

How can I extract the value inside a div using xpath or css_selector? This is the HTML markup: <div class="catalog-products view-tile" data-catalog-products="" data-slider-available="" data-primary-as-icon=""> ...

Showing the heading again after a new page starts

Currently, I am using WeasyPrint to generate a document and facing an issue with long sections that may span multiple pages. When a section breaks across pages, the name of the section does not display after the page break as intended. The following examp ...

Utilizing un-packaged Python scripts from GitHub

I'm currently attempting to implement the code found in this repository into my project. However, it appears that this code does not exist as a package in PyPI and lacks a setup.py file, making the typical installation process with pip install git+< ...

Error in Python caused by mathematical domain limits

This particular program is designed to work effectively when the variable "n" is set to 4, as demonstrated in the code snippet below: from __future__ import division from numpy import zeros import numpy as np import matplotlib.pyplot as plt from numpy.li ...

Save the solutions obtained from SymPy's solve() function in a convenient format

I created a for-loop to calculate multiple angles and stored all the results in a NumPy array of dictionaries. However, I am facing challenges in converting all the answers to degrees (instead of radians) and rounding them. The problem seems simple, but ob ...

Creating a matrix in Python: A step-by-step guide

I am having an issue with creating a 2x3 matrix. Every time I execute my code, the matrix is displayed within parentheses, which is not the desired outcome. def fill_matrix(numrows, numcols, val): matrix = [[val for i in range(numrows)] for j in rang ...

How to automate clicking multiple buttons on the same webpage using Selenium with Python

As a Python and Selenium novice utilizing chromedriver, I find myself in need of assistance. The task at hand involves a web page that is unfortunately restricted from being accessed externally. This particular webpage hosts approximately 15 buttons with ...

Verifying the presence of a file within a specified list of directories

I am currently working on a project that involves checking if a specific file exists within any of the directories listed. If the file is found, the code should return True; otherwise, it should return False. I have encountered some difficulties in the pro ...

I am encountering a 404 error when attempting to make a GET request for a file located in the same directory

Here is the Javascript code that utilizes the get method. The directory contains both files - the HTML file where the JS code resides, and the text file. Below is an image of the console displaying errors. ...

Improving List Comprehension Efficiency

Recently, I created a Python script that involves two custom classes - a 'Library' class (Lib) containing a list of objects based on a 'Cas' class. The specifics of these classes are not provided here, but what you need to know is that ...

Keep looping until the input of the function transforms

I have python code that produces a continuous stream of values, and I want to convert these values into audio feedback using a buzzer with pauses between beeps based on the value stream. There's another piece of python code available below that gener ...