Skip to content
Advertisement

Tag: nan

How to merge two dataframe in pandas to replace nan

I want to do this in pandas: I have 2 dataframes, A and B, I want to replace only NaN of A with B values. Answer The official way promoted exactly to do this is A.combine_first(B). Further information are in the official documentation. However, it gets outperformed massively with large databases from A.fillna(B) (performed tests with 25000 elements):

NumPy: calculate averages with NaNs removed

How can I calculate matrix mean values along a matrix, but to remove nan values from calculation? (For R people, think na.rm = TRUE). Here is my [non-]working example: With NaNs removed, my expected output would be: Answer I think what you want is a masked array: Edit: Combining all of the timing data Returns:

Python: sort function breaks in the presence of nan

sorted([2, float(‘nan’), 1]) returns [2, nan, 1] (At least on Activestate Python 3.1 implementation.) I understand nan is a weird object, so I wouldn’t be surprised if it shows up in random places in the sort result. But it also messes up the sort for the non-nan numbers in the container, which is really unexpected. I asked a related question

Advertisement