0

I've got df as follows:

   a    b
0  1  NaN
1  2  NaN
2  1  1.0
3  4  NaN
4  9  1.0
5  6  NaN
6  5  2.0
7  8  NaN
8  9  2.0

I'd like fill nan's only between numbers to get df like this:

   a    b
0  1  NaN
1  2  NaN
2  1  1.0
3  4  1.0
4  9  1.0
5  6  NaN
6  5  2.0
7  8  2.0
8  9  2.0

and then create two new dataframes:

   a    b
2  1  1.0
3  4  1.0
4  9  1.0
   a    b
6  5  2.0
7  8  2.0
8  9  2.0

meaning select all columns and rows with fiiled out nan only.

My idea for first part, this with filling out nan is to create separate dataframe with row indexes like:

2 1.0
4 1.0
6 2.0
8 2.0

and based on that create range of row indexes to fill out.

My question is maybe there is, related to this part with replacing nan, more pythonic function to do this.

data_b77
  • 415
  • 6
  • 19
  • Is the filling between all rows bounded by the same number, or is it just any NaNs between two non-null values. – ALollz Jan 10 '20 at 21:59

1 Answers1

2

How about

df[df.b.ffill()==df.b.bfill()].ffill()

results in

#    a    b
# 2  1  1.0
# 3  4  1.0
# 4  9  1.0
# 6  5  2.0
# 7  8  2.0
# 8  9  2.0

Explanation:

df['c'] = df.b.ffill()
df['d'] = df.b.bfill()

#    a    b    c    d
# 0  1  NaN  NaN  1.0
# 1  2  NaN  NaN  1.0
# 2  1  1.0  1.0  1.0
# 3  4  NaN  1.0  1.0
# 4  9  1.0  1.0  1.0
# 5  6  NaN  1.0  2.0
# 6  5  2.0  2.0  2.0
# 7  8  NaN  2.0  2.0
# 8  9  2.0  2.0  2.0
SpghttCd
  • 10,510
  • 2
  • 20
  • 25