Home > OS >  How to create a new dataframe from another one where cells (not entire row/column) not meeting certa
How to create a new dataframe from another one where cells (not entire row/column) not meeting certa

Time:06-24

How to delete the cells in the df below based on the following condition: - If Size_2 - Size_1 <= 2 and Height_2 <= Height_1, then delete Size_2 and Height_2

Sample Size_1 Height_1 Size_2 Height_2
A 10 120 12 110
B 12 150 20 140
C 14 170 15 160
D 16 180 22 150

the output will look like this:

Sample Size_1 Height_1 Size_2 Height_2
A 10 120
B 12 150 20 140
C 14 170
D 16 180 22 150

Thank you very much for your help!

CodePudding user response:

You can do this with a mask:

df.loc[(df["Size_2"] - df["Size_1"] <= 2) & (df["Height_2"] <= df["Height_1"]), ["Size_2", "Height_2"]] = np.NAN

Note that you can replace the np.NAN with anything you want.

  • Related