I'm trying to remove NaN values from a dataframe (without removing the entire column or row) and shift the next value to the prior column. Example:
CLIENT| ANIMAL_1 | ANIMAL_2 | ANIMAL_3| ANIMAL_4
ROW_1 1 | cow | frog | NaN | dog
ROW_2 2 | pig | NaN | cat | NaN
My goal is to have:
CLIENT| ANIMAL_1 | ANIMAL_2 | ANIMAL_3| ANIMAL_4
ROW_1 1 | cow | frog | dog | NaN
ROW_2 2 | pig | cat | NaN | NaN
What I have tried:
Transforming each row into lists and deleting the NaN from each one. But I can't seem to delete the values from the list:
x = df[df.CLIENT == 1].iloc[:,1:].values.tolist()
then I get:
[['cow', 'frog', nan, 'dog']]
to delete the 'nan' I tried:
row_without_nan = [animal for animal in x if str(animal) != 'nan']
But it doesn't change anything in the list. I tried changing the nulls to another word and using that word, but it didn't work either.
- Transforming each row into array. I tried transforming into array by using
np.array(), but it is useless, because the nulls become'nan'and when I try to usenp.isnanI get this:TypeError: ufunc 'isnan' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''
Does anyone know what I'm doing wrong with my list or if there is a smarter/faster way to do it?