Issue
I have a dataframe with NaN. I have to remove nan at the starting rows only, and wants to keeps NaN after real number starts:
Suppose my data frame is something like:
a = pd.DataFrame({'data':[np.nan,np.nan,np.nan,np.nan,4,5,6,2,np.nan,1,3,4,5,np.nan,4,5,np.nan,np.nan]})
a=
data
0 NaN
1 NaN
2 NaN
3 NaN
4 4.0
5 5.0
6 6.0
7 2.0
8 NaN
9 1.0
10 3.0
11 4.0
12 5.0
13 NaN
14 4.0
15 5.0
16 NaN
17 NaN
and I tried to remove NaN at the beginning and wants data-frame like this:
data
4 4.0
5 5.0
6 6.0
7 2.0
8 NaN
9 1.0
10 3.0
11 4.0
12 5.0
13 NaN
14 4.0
15 5.0
16 NaN
17 NaN
I tried to use this function but it is not working.
for w in np.arange(len(a)):
if a.iloc[w] == np.nan:
a.drop(a.index[w])
Solution
Get the first valid index and slice
idx = a.first_valid_index()
a.loc[idx:]
data
4 4.0
5 5.0
6 6.0
7 2.0
8 NaN
9 1.0
10 3.0
11 4.0
12 5.0
13 NaN
14 4.0
15 5.0
16 NaN
17 NaN
Answered By - Vaishali
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.