I want to replace DF1.index.values with DF2[some_column].values HOWEVER i only want to replace if DF2[some_column].value is not null or empty string.
DF1.index.values
Index(['a','b','c','d']), dtype='object')
DF2[some_column].values
['base','','','net 1']
Expected output
Index(['base','b','c','net 1']), dtype='object')
My attempt:
DF1.index = DF2[some_column].values
It's incorrect because it replaces everything and thats not what I want, I'm only interested in not null or empty values.
You can use
where
to choose values by the condition from to sources:So it will use
DF2[some_column]
values ifDF2[some_column]!=""
(in fact here we create a Series which is used as boolean index saying what data to choose for each raw), in the other case it will useDF2.index
value.As you don't want
nan
values also, you needpd.notnull
functions. And to check both conditions use "logical and" function for pairs of values in two Series which is&
.So the final code is