首页 > 解决方案 > How to filter out rows in Table based on the row values?

问题描述

I'm quite a beginner with python and have a very basic question. So here, I have created a table like below.

A = make_array("a", "b", "c", "d", "e", "f", "g")
B = make_array("No", "10%", "5%", "No", "15%", "21%", "3%")
test = Table().with_columns("A", A, "B", B)

I want to filter out the rows that contain "No" and leave only those with integer percentage values. I have tried using where function, but didn't work.

test_new = test.where("B" != "No")

Could anyone guide me through how I could do it? As an end goal, I want to have a table with only b,c,e,f,g.

Edit: I'm using Numpy library and datascience pacakge.

标签: pythonnumpydata-science

解决方案


我建议你使用pandas它。你可以做类似的事情,

import pandas as pd

# Create a df with some example data
df = pd.DataFrame({"A": ["a", "b", "c", "d", "e", "f", "g"], "B": ["No", "10/%", "5/%", "No", "15/%", "21/%", "3/%"]})

# Only select rows for which column B is not "No". 
df['A'][~df['B'].str.contains("No")]

注意这里我使用contains()了 ,所以如果单元格包含子字符串“No”,它也会被选中。如果您想进行精确比较,可以执行以下操作:

df = pd.DataFrame({"A": ["a", "b", "c", "d", "e", "f", "g"], "B": ["No", "10/%", "5/%", "No", "Non", "21/%", "3/%"]})

df['A'][~(df['B'] ==  "No")]

推荐阅读