Home > Blockchain >  Drop columns based on row -index 0
Drop columns based on row -index 0

Time:10-25

I am new to python and I am trying to drop some columns in a Dataframe based on the variable in row 0. The dataframe looks like this:

                                C00         C01         C02         C03  
0      Time(s)/Cell Status    accepted    accepted    accepted    accepted   
1                        0     40.1782   -245.4999    15.96771    53.06083   
2                 0.049961     161.299   -357.6592   -263.0684   -52.77648   
3                 0.099922    244.7165   -338.4682   -298.6496   -34.78166   
4                 0.149883    242.8061   -291.4323   -129.1347   -32.34716   

The df has many more columns. I am basically trying to remove the columns where row 0 = rejected. Thanks a lot for your help.

CodePudding user response:

Do you mean something like:

print(df.loc[:,df.iloc[0]=="accepted"])

CodePudding user response:

This would return the dataframe after removing the columns:

df.drop(df.columns[df.iloc[0] == "rejected"], axis=1)

If you want to change the original dataframe, then this:

df.drop(df.columns[df.iloc[0] == "rejected"], axis=1, inplace=True)

CodePudding user response:

You could extract to columns and then drop them like this:

col_to_remove = list(df.iloc[0][df.iloc[0]=="rejected"].index)

df.drop(col_to_remove, 1)
  • Related