0

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.

2
  • does DF2[some_column].replace('', DF1.index) work? Commented Jan 22, 2016 at 13:05
  • Thanks EdChum but it wouldnt do fulfill what I plan to do. Commented Jan 22, 2016 at 16:47

1 Answer 1

1

You can use where to choose values by the condition from to sources:

DF1.index = DF2[some_column].where(DF2[some_column]!="", DF2.index)

So it will use DF2[some_column] values if DF2[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 use DF2.index value.

As you don't want nan values also, you need pd.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

DF1.index = DF2[some_column].where(
           (DF2[some_column]!="") & pd.notnull(DF2[some_column]), DF2.index)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.