So for a survey dataset I have, a question goes " q19: rank what the top three are for you based on the following" and lets say the following would be 13 options. The value that will be in a given row is the rank that a given respondent has given to that option. In the dataset it looks like this, abbreviated the columns but this below should go to option13:
| option1 | option2 | option3 | option4 |
|---|---|---|---|
| blank | blank | blank | blank |
| 2 | blank | 3 | 1 |
| 3 | blank | 1 | blank |
Now I would like to remap this column setup in the data so that instead of having x columns for each option I could consolidate the columns down into 3 or rank1, rank2, rank3. So far I've created a conditions list to check if any of the columns I want to match against contain a given value ranking by using the or operator. This is ok logic because only one option can be selected for each rank.
q19_cond = [
(df['Q19r1'] == 3) | (df['Q19r2'] == 3) | (df['Q19r3'] == 3) | (df['Q19r4'] == 3) | (df['Q19r5'] == 3) | (df['Q19r6'] == 3) | (df['Q19r7'] == 3) | (df['Q19r8'] == 3) | (df['Q19r9'] == 3) | (df['Q19r10'] == 3) | (df['Q19r11'] == 3) | (df['Q19r12'] == 3) | (df['Q19r13'] == 3),
(df['Q19r1'] == 2) | (df['Q19r2'] == 2) | (df['Q19r3'] == 2) | (df['Q19r4'] == 2) | (df['Q19r5'] == 2) | (df['Q19r6'] == 2) | (df['Q19r7'] == 2) | (df['Q19r8'] == 2) | (df['Q19r9'] == 2) | (df['Q19r10'] == 2) | (df['Q19r11'] == 2) | (df['Q19r12'] == 2) | (df['Q19r13'] == 2),
(df['Q19r1'] == 1) | (df['Q19r2'] == 1) | (df['Q19r3'] == 1) | (df['Q19r4'] == 1) | (df['Q19r5'] == 1) | (df['Q19r6'] == 1) | (df['Q19r7'] == 1) | (df['Q19r8'] == 1) | (df['Q19r9'] == 1) | (df['Q19r10'] == 1) | (df['Q19r11'] == 1) | (df['Q19r12'] == 1) | (df['Q19r13'] == 1)
]
#function attempt
def rank_select(row):
np.select(q19_cond, df.column)
Ultimately, I need to create a function that checks against those conditions and appends the column name appropriately to the correct rank column.
Aucun commentaire:
Enregistrer un commentaire