Pandas: Selecting rows and columns based on a subset of columns that contain a certain value

2024/11/18 11:44:33

Let's say I have a dataframe with column names as follows:

col_id_1, col_id_2, ..., col_id_m, property_1, property_2 ..., property_n

As an example, how would I search across all col_ids for, say, the value 5 (note that 5 won't appear in multiple col_ids in the same row), and then choose all rows that contain this value? On top of that, once I've found all rows that have a col_id containing the value 5, I'll combine all the col_ids with value 5 into a single id column, and also only choose, say, property_8 and property_25000 as additional columns.

In this case, I would have a table with the following columns:

id, property_8, property_25000

where the id column only contains rows with value 5. Is such a thing possible in pandas?

Answer

IIUC, first filter you columns by contain the col_id, then we using any check if any columns have the number 5

df.loc[df.filter(like='col_id').eq(5).any(1),['property_8','property_25000']].assign(id=5)
https://en.xdnf.cn/q/118680.html

Related Q&A

Open a image file then display it in new window

I have a button in my GUI then after selecting a image file I want it to display in new window.Code:import tkinter as tk from tkinter import * from tkinter import ttk from tkinter import filedialog …

python plotting chart in interactive viewer vscode

This works and shows a plot in vscode: #%% cell with plot import matplotlib.pyplot as plt y = [3.2, 3.9, 3.7, 3.5, 3.02199] x = [0.15, 0.3, 0.45, 0.6, 0.75] n = [155, "outliner", 293, 230, 67…

Find all lines in a dataframe that matches specific pattern and extract the strings after split

I have a dataframe that looks like LineEntry: [0x0000000002758261-0x0000000002758268): /a/b/c:7921:14 LineEntry: [0x0000000002756945-0x0000000002756960): /f/b/c:6545:10 LineEntry: [0x00000000027562c9-0…

Python: Concatenate many dicts of numpy arrays with same keys and size

I have a function called within a loop that returns a dict (dsst_mean) with roughly 50 variables. All variables are numpy arrays of length 10.The loop iterates roughly 3000 times. Im current concatenat…

intersection of 2 objects of different types

i want to take the intersection of: ((e, 13.02338360095244), (a, 11.820318700775383), (o, 9.20172171683253), (s, 7.635081506807498), (n, 7.547469320471335), (i, 7.219915745772025), (r, 6.70492704072287…

Enemy Projectiles Attack Way To Fast Problem

I am trying to make my enemy bullets attack the player but its attacking way to fast I dont know why VIDEO my enemy bullets class# enemys bulletsksud = pygame.image.load("heart.png")class Boo…

How to find the maximum digit of an integer? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.Want to improve this question? Add details and clarify the problem by editing this post.Closed 10 years ago.This p…

How to use python to generate a magazine cover? [closed]

Its difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying thi…

Why do I get this error? NoneType object has no attribute shape in opencv

Im working on real-time clothing detection. so i borrowed the code from GitHub like this:https://github.com/rajkbharali/Real-time-clothes-detection but (H, W) = frame.shape[:2]:following error in last …

Efficiently concatenate two strings from tuples in a list?

I want to concatenate the two string elements in a list of tuplesI have this:mylist = [(a, b), (c, d), (e, f), (g, h)] myanswer = []for tup1 in mylist:myanswer.append(tup1[0] + tup[1])Its working but i…