Jaden-Dz99
Jaden-Dz99

Reputation: 139

Drop a cell in column based on value

A basic example of my current data_frame looks like:

Material Description    From
1 Bitumen               0.025
2 Road                  0.14
3 Filling               0.24
4                       0.82
5                       1.55
6 Filling
7 Sand

And I would like to have this data_frame output below:

Material Description    From
1 Bitumen               0.025
2 Road                  0.14
3 Filling               0.24
4 Filling               0.82
5 Sand                  1.55

It would be great to iterate through these cells in the data_frame['Material Description'] and drop the cells and not the entire row. I have tried the code below but unfortunately an Attribute Error occurs.

for q in range(len(data_frame)):
    if (data_frame.loc[q, "Material Description"]) == "":
        data_frame.loc[q, "Material Description"].drop()

Thanks in advance for your help! :)

Upvotes: 0

Views: 39

Answers (1)

BENY
BENY

Reputation: 323226

This is more like a NaN shift problem , in order to speed up you can check Python: Justifying NumPy array

df=df.mask(df=='').apply(lambda x : sorted(x,key=pd.isnull)).dropna(thresh=1)
df
Out[145]: 
  MaterialDescription   From
1             Bitumen  0.025
2                Road  0.140
3             Filling  0.240
4             Filling  0.820
5                Sand  1.550

Upvotes: 1

Related Questions