Home > Mobile >  Positional indexing with NA values
Positional indexing with NA values

Time:04-08

I need to index the dataframe from positional index, but I got NA values in previous operation and I wanna preserve it. How could I achieve this?

df1

NaN
1
NaN
NaN
NaN
6

df2

0 10
1 15
2 13
3 15
4 16
5 17
6 17
7 18
8 10

df3

0 15
1 17

The output I want

NaN
15
NaN
NaN
NaN
17

df2.iloc(df1)

IndexError: indices are out-of-bounds

.iloc method in this case drive to a unbound error, I think .iloc is not available here. df3 is another output generated by .loc, but I don't know how to add NaN between them. If you can achieve output by using df1 and df3 is also ok

CodePudding user response:

If df1 and df2 has same index values use for replace non missing values by values from another DataFrame DataFrame.mask with DataFrame.isna:

df1 = df2.mask(df1.isna())
print (df1)
    col
0   NaN
1  15.0
2   NaN
3   NaN
4   NaN
5  17.0
  • Related