0

I have been working on large dataset with Lat & Longitude Data. I am doing interpolation for the whole df, so before that i just want my latitude and longitude column to have elements to filled with values without zeros.

My Dataframe (explanation purpose) :

Time  lat  long
0  0  0
1  0  0
2  0  0
3  0  0
4  0  0
5  0  0
6  0  0
7  5  2
8  5  2
9  0  0
10  0  0
11  0  0
12  0  0
13  6  1
14  6  1

My Requirement :

    Time  lat  long
    0  5  2
    1  5  2
    2  5  2
    3  5  2
    4  5       2
    5  5  2
    6  5  2
    7  5  2
    8  5  2
    9  6  1
    10  6  1
    11  6  1
    12  6  1
    13  6  1
    14  6  1

What I needed

I hope you understand from the actual df and my requirement.

I just want to take the next last element which has proper values other than zero & fill that element in previous rows of that column.

Request

I believe that there will be some nice methods to complete my work done..

Mari
  • 698
  • 1
  • 8
  • 27

1 Answers1

2

You could replace all 0s with np.nan and use bfill to fill them with the next valid value:

df.loc[:,'Time':].replace(0,np.nan).bfill()

Time  lat  long
0    1.0  5.0   2.0
1    1.0  5.0   2.0
2    2.0  5.0   2.0
3    3.0  5.0   2.0
4    4.0  5.0   2.0
5    5.0  5.0   2.0
6    6.0  5.0   2.0
7    7.0  5.0   2.0
8    8.0  5.0   2.0
9    9.0  6.0   1.0
10  10.0  6.0   1.0
11  11.0  6.0   1.0
12  12.0  6.0   1.0
13  13.0  6.0   1.0
14  14.0  6.0   1.0
yatu
  • 86,083
  • 12
  • 84
  • 139