python - Use a variable number of conditions to select rows whose column value equals some value
问题描述
I want to get a subset of a dataframe based upon multiple conditions with the number of conditions I pass it variable.
I have seen similar answers with multiple conditions(Select rows from a DataFrame based on values in a column in pandas) , but none that allow to pass less variables.
I have tried using: c=None, c=True, c=all, but it always evaluates to false
def Subset (df, a=None, b=None, c=True):
temp=df.loc[(df['a'] == a) & (df['b'] == b) & (df['c'] == c)]
return (temp)
if I evaluate:
Subset=Subset(df=Table, a=350, b=300)
I get an empty dataframe
while if I use the function:
def Try(df, a=None, b=None):
temp=df.loc[(df['a'] == a) & (df['b'] == b)]
return (temp)
I get a dataframe with 10 rows.
To answer Yaakov Bressler's comment I am giving more information: My dataframe looks like this:
files,Names,Curve Type,Thickness,Temperature,Number,Iteration,leak,start,stop,Vth,F_E_M,on/off
Output [(1) _250-300-G21_]0.csv,250-300-G21,Output,250,300,G21,0,True,,,,,
Output [(1) _250-300-G22_]0.csv,250-300-G22,Output,250,300,G22,0,False,,,,,
Transfer lin [(1) _250-300-G21_;]0.csv,250-300-G21,Transfer lin,250,300,G21,0,True,,,,,
the first column are the filenames. the other columns are data about the transitor that file represents.
I want to create a subset of this file representing a single transistor, defined by: ( Curve Type,Thickness,Temperature,Number ) or of a single chip : (Curve Type,Thickness,Temperature).
This is so that I can import them and do plots/analysis.
解决方案
Seems like your dataframe's 'c'
column is not a boolean. Try print(df['c'].dtype == 'bool')
Also, sharing your original df and the goal would help elucidate your problem.
Also, also, I would not name an object after a function as in the case of Subset=Subset(df=Table, a=350, b=300)
推荐阅读
- javascript - Animejs: translateX 0 值不起作用
- android - 使用循环将多个视图添加到 RelativeLayout
- api - 如何在运输标签上添加有关货物危险的信息?
- git - Visual Studio 2017“连接到项目”显示错误的服务器存储库
- environment-variables - Electron - 重启应用程序/刷新环境变量
- reactjs - 在reactjs上创建带有左侧文本和右侧的图像/图标的按钮
- javascript - 有什么方法可以检测用户是否从本地存储中删除了任何项目
- excel - 包含字符串和数字的列到仅包含数字的列
- c# - 正则表达式长度如何工作?
- apache-spark - XGBoost-Spark 在训练期间做什么以及如何优化其资源使用?