python - 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.
解决方案
我建议你使用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")]
推荐阅读
- python - TensorFlow 2.0:如何控制已保存模型的名称?
- cucumber-jvm - 如何在基于环境的黄瓜场景大纲示例中传递参数?
- c++ - 验证没有字符和负输入的输入
- reactjs - Log4js-node 找不到集群模块
- azure-devops - 如何在 Azure Devops 中显示显示构建指标的仪表板
- swift - UITableview 与单元格两个单元格
- javascript - 如何使用 autoColumns 添加标题过滤器?
- css - 如何使用 WordPress 子主题/css 定制器替换所有具有该值的 css 类/属性中的特定属性值
- python - 定义一个相对的首选函数 - python turtle
- apache-spark - 如何优化 Spark 结构化流应用程序中的执行程序实例数量?