Skip to content
Advertisement

How to delete empty spaces from pandas DataFrame rows until first populated field?

Lets say I imported a really messy data from a PFD and I´m cleaning it. I have something like this:

Name Type Date other1 other2 other3
Name1 Type1 Date1
Name2 Type2 Date2
Name3 Type3 Date3
Name4 Type4 Date4
Name5 Type5 Date5

And so on. As you can see, Type is always before date on each row, but I basically need to delete all ” (currently empty strings on the DataFrame) while moving everything to the left so they align with their respective Type and Date columns. Additionally, there’s more columns to the right with the same problem, but for structural reasons I cant remove ALL ”, the solution I´m looking for would just move ‘everything to the left’ so to speak (as it happens with pd.shift).

I appreciate your help.

Advertisement

Answer

What worked for me was:

while '' in df['Type'].unique():
    for i,row in df.iterrows():
        if row['Type'] == '':
            df.iloc[i, 1:] = df.iloc[i, 1:].shift(-1, fill_value='')

And the same for next column

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement