Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Positional indexing with NA values

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

>Solution :

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading