0

I would like to merge two binary columns in one, taking max value. I found similar questions, but I would like to ask if there is a chance to find a solution when you don't know the names of your columns (so it could be in a generic loop) .

    for key, value in duplicates_binary_questions.items():

    value = [str(x) for x in value]
    sub_data_binary = data[
        data.columns[np.isin([x[: x.find("_")] for x in data.columns], value)]
    ]

duplicates_binary_questions = { "question" : [114, 155] }

I have a dictionary with ids (value) of duplicated questions (key). I'm creating an object (sub_data_binary) which are ids of duplicated questions from dictionary aligned with my main data (where I have all questions). What I want to do is to join/melt two binary columns (where columns can have different names) into one, taking the max value (join columns within sub_data_binary).

I would appreciate for any suggestions :)

EDIT: I' ve found a solution - I've used sub_data_binary.max(axis=1) - I did try to use it before asking this question, but I had an error - It turned out that I didn't rename sub_data_binary, so then I had a problem to merge it with my main dataframe :D

  • 1
    Please provide a [Minimal Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example) especially for [Pandas](https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples) – Corralien Jun 10 '22 at 06:12
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community Jun 10 '22 at 15:48

0 Answers0