0

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! :)

Jaden-Dz99
  • 119
  • 8

1 Answers1

1

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
BENY
  • 317,841
  • 20
  • 164
  • 234