Reading a column with multiple values separated by a semicolon

Hello Python Family

Please help me.

I have a column (Medication.Medication) with a list of 4-10 medications separated by a semicolon.
I am trying to group the medications based on their respective class.
I have created a new column (Beta) for betablockers. I want the value in the Beta column to change from 0 to 1 if a patient is taking Atenolol or Carvedilol.

df2[‘Beta’] = 0
df2[‘Beta’] = df2[‘Medication.Medication’].where(df2[‘Medication.Medication’] == “Atenolol”, “Carvedilol”, other=0)

I am not certain how to specify in the code that the output should be “1” if the column includes Atenolol or Carvedilol. The above code returns “0” for all indexes.

Kindly assist me. Thank you.

I’m guessing you’re using pandas? It is helpful to include that kind of
information. The docs for the .where method seem to be here:

You’ve written you test incorrectly. You wrote:

.where(df2['Medication.Medication'] == "Atenolol", "Carvedilol", other=0)

Because of the comma, that supplies these 3 parameters:

df2['Medication.Medication'] == "Atenolol"

which means that (a) you are only testing against “Atenolol” and (b)
you’re specifying “Carvedilol” as the “other” parameter. And also
specifying 0 as the “other” parameter.

In reflection, I am surprised that this is even accepted by Python.
Please always provide code directly copied from your running

You need to ensure the entire test is one parameter. For example:

.where(df2['Medication.Medication'] in ("Atenolol", "Carvedilol"), other=0)

which supplies these 2 parameters:

df2['Medication.Medication'] in ("Atenolol", "Carvedilol")

Also notice that the test checks for the medication being in a tuple of
possible values. A common beginner mistake in Python is to try to write
such a test like this:

df2['Medication.Medication'] == "Atenolol" or "Carvedilol"

which does not mean what is wanted. instead, that is read by Python as:

df2['Medication.Medication'] == "Atenolol"

Cameron Simpson

Dear Cameron

Thank you so much for taking the time to assist me. I appreciate the simple explanation and your overall input. You are a star.