0

I am trying to implement the following logic on a python dataframe.

If the string value in Column A is in a list that I pre-designated, then replace Column B with the value in Column A. Otherwise, leave the value in column B as is.

The closest I can get right now is a wonky np.where statement that isn't quite what I want and errors out. Please see below.

enter image description here

Any help is greatly appreciated

3
  • Pandas or python? Also rather write out the code than take a screenshot. Commented Nov 10, 2017 at 1:55
  • You mean a Pandas DataFrame? Try indexing using pandas.DataFrame.isin Commented Nov 10, 2017 at 2:12
  • Please add code and data as text (using code formatting), not images. Images: A) don't allow us to copy-&-paste the code/errors/data for testing; B) don't permit searching based on the code/error/data contents; and many more reasons. Images should only be used, in addition to text in code format, if having the image adds something significant that is not conveyed by just the text code/error/data. Commented May 3, 2019 at 7:55

1 Answer 1

1

There are a lot of way in which this can be done. One way and probably the easiest way would be

column_to_list = df['A'].tolist()
for index, value in enumerate(column_to_list):
    if value in Other_list:
        df.iloc['B'][index] = value        
    else:
        pass

If this meets you expectation let us know and please never post Photo of codes

Sign up to request clarification or add additional context in comments.

2 Comments

Replace Other_list with your pre-designated list.
@PineNuts0 If it solved your problem please accept the answer. In case it didn't let us know so that we can provide better answers

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.