0

Tengo un Dataframe y me gustaria hacer otra columna que que Combinen las columnas cuyo nombre comienza con la misma value en Answer y QID.

Es por decir, teniendo el siguiente Dataframe

    QID     Category    Text    QType   Question:   Answer0     Answer1     Country
0   16  Automotive  Access to car   Single  Do you have access to a car?    I own a car/cars    I own a car/cars  UK
1   16  Automotive  Access to car   Single  Do you have access to a car?    I lease/ have a company car     I lease/have a company car  UK
2   16  Automotive  Access to car   Single  Do you have access to a car?    I have access to a car/cars     I have access to a car/cars     UK
3   16  Automotive  Access to car   Single  Do you have access to a car?    No, I don’t have access to a car/cars   No, I don't have access to a car    UK
4   16  Automotive  Access to car   Single  Do you have access to a car?    Prefer not to say   Prefer not to say   UK

Necesito Obtener como Resultado:

        QID     Category    Text    QType   Question:   Answer0     Answer1     Answer2    Answer3  Country    Answers
    0   16  Automotive  Access to car   Single  Do you have access to a car?    I own a car/cars    I lease/ have a company car      I have access to a car/cars    No, I don’t have access to a car/cars    UK    ['I own a car/cars', 'I lease/ have a company car'   ,'I have access to a car/cars', 'No, I don’t have access to a car/cars', 'Prefer not to say     Prefer not to say']

Hasta ahora tengo lo siguiente:

# lazy - want first of all attributes except QID and Answer columns
agg = {col:"first" for col in list(df.columns) if col!="QID" and "Answer" not in col}
# get a list of all answers in Answer0 for a QID
agg = {**agg, **{"Answer0":lambda s: list(s)}}

# helper function for row call.  not needed but makes more readable
def ans(r, i):
    return "" if i>=len(r["AnswerT"]) else r["AnswerT"][i]

# split list from aggregation back out into columns using assign
# rename Answer0 to AnserT from aggregation so that it can be referred to.  
# AnswerT drop it when don't want it any more
dfgrouped = df.groupby("QID").agg(agg).reset_index().rename(columns={"Answer0":"AnswerT"}).assign(
    Answer0=lambda dfa: dfa.apply(lambda r: ans(r, 0), axis=1),
    Answer1=lambda dfa: dfa.apply(lambda r: ans(r, 1), axis=1),
    Answer2=lambda dfa: dfa.apply(lambda r: ans(r, 2), axis=1),
    Answer3=lambda dfa: dfa.apply(lambda r: ans(r, 3), axis=1),
    Answer4=lambda dfa: dfa.apply(lambda r: ans(r, 4), axis=1),
    Answer5=lambda dfa: dfa.apply(lambda r: ans(r, 5), axis=1),
    Answer6=lambda dfa: dfa.apply(lambda r: ans(r, 6), axis=1),
).drop("AnswerT", axis=1)

print(dfgrouped.to_string(index=False))

Funciona bien pero hay veces que tengo más de seis respuestas. ¿Cómo lo haces dinámico en términos del número de respuestas dadas a un par de (QID, Question:)?

1 respuesta 1

0
df.groupby(COLUMNAS_CRITERIO)[COLUMNA_VALOR].apply(list)

Aquí COLUMNAS_CRITERIO es la lista de columnas que vas a agrupar (value, Answer y QID), y COLUMNA_VALOR es la columna con el valor que se va a repetir (Answers?).

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge you have read our privacy policy.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.