skip columns in python

424 Views Asked by At

I have a dataframe with many columns and I used the following code to convert each column into a list. Then I need to check if the first value in each list equals 0, if yes, then skip this list and not running the following function (baseline removal function in my case). But I still want to keep this list in my final dataframe. I tried to used the if...continue function, it can skip this list but cannot keep this original list. Is there any way to achieve that?

Master=[]

for i in df.columns:
temp = df[i].values.tolist()
if temp [0] == 0:
    continue
baseObj=BaselineRemoval(temp)
Zhangfit_output=baseObj.ZhangFit()
Master.append(Zhangfit_output)

output = pd.DataFrame (Master)
1

There are 1 best solutions below

0
On

Use append function first and then continue function

Master=[]
for i in df.columns:
temp = df[i].values.tolist()
if temp [0] == 0:
    Master.append(temp)   
else:
    baseObj=BaselineRemoval(temp)
    Zhangfit_output=baseObj.ZhangFit()
    Master.append(Zhangfit_output)


output = pd.DataFrame (Master)