2

I would like to assign names for the new column "SubtestName" based on the "SubtestID" column.

Currently my codes are as follows:

conditions = [(df4['subtestID'] == 325)|(df4['subtestID'] == 341)|(df4['subtestID'] == 1164)|(df4['subtestID'] == 1200),
              (df4['subtestID'] == 347)|(df4['subtestID'] == 357)|(df4['subtestID'] == 1308)|(df4['subtestID'] == 1330),
              (df4['subtestID'] == 328)|(df4['subtestID'] == 344)|(df4['subtestID'] == 1167)|(df4['subtestID'] == 1203)]

values = ["TestName1","TestName2","TestName3"]

df4['subTestName'] = np.select(conditions, values)

I would like to rewrite my codes in a better way without repeating "df4['subtestID']" every time I want to assign a new ID. I am planning to assign another 30 subtestnames.

I tried using this way but it gave me an error.

df4['subtestID'] in (325,341,1164,1200) 

ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all()

Any other methods I can use to assign the names to the SubtestID?

2 Answers2

1

You could try changing your code to:

s_id = df4['subtestID']
conditions = [s_id.isin(325,341,1164,1200),
              s_id.isin(347,357,1308,1330),
              s_id.isin(328,344,1167,1203)]

values = ["TestName1","TestName2","TestName3"]
df4['subTestName'] = np.select(conditions, values,'other name')
sophocles
  • 13,593
  • 3
  • 14
  • 33
0

You can simply us isin() as follows:

df4['subtestID'].isin([325,341,1164,1200]) 
Antoine Dubuis
  • 4,974
  • 1
  • 15
  • 29