2

Tengo una dataframe con una columna Questions y una otra QTypes. Me gustaria que cada linea que encontramos [''] (es decir [ Apóstrofo Apóstrofo]) en Questions puestamos Sections en QTypes.

    QID Questions   Answers QType
0   H1  When do you think your next vacation can start? ['In next 3 months', 'In next 6 months', 'In next 1 year', 'Only once COVID-19 is under control', 'Only once COVID-19 vaccine is developed']    Multiple Choice
1   H2  What are your preferences regarding medical treatment policy (with additional cost)?    ["Doctor's availability in hotel", 'Ventilator availability in hotel', 'Tie-ups with nearby hospitals', 'Availability of medical rooms with primary first aid care']    Multiple Choice
2   H3  What is your preferences of complementary breakfast?    ['Buffet breakfast with social distancing', 'Buffet breakfast replaced with Ala-carte with limited options', 'Breakfast to be delivered in room with limited options (chargeable)', 'Packaged breakfast only']  Multiple Choice
3   H4  What is your preference for a in-hotel grocery shops for the basic necessity items and packaged food?   ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10'] Likert Scale
4   H5  Consumer Personality    ['']    Multiple Choice
5   H6  What is your preference of hotel check-in?  ['Collect keys at the counter maintaining social distancing', 'Collect keys at the KIOSK using booking bar-code', 'Online Keys using the mobile App']   Multiple Choice
6   H7  What is your preference of payment during Check-out?    ['Pay at the counter maintaining social distancing', 'Pay at KIOSK', 'Online payment using the mobile App'] Multiple Choice
7   H8  What is your preference of hotel cancellation / travel date change policy?  ['Travel date change is preferred at no cost', 'Cancellation at some minimal cost (based on hotel policy)', 'Cancellation with some amount refund and hotel coupons for next visit']    Multiple Choice
8   H9  What is your preference of the guest policy?    ['Guests are allowed in living room with precautions', 'Guest are allowed only in certain designated areas', 'No guests are allowed inside hotel']  Multiple Choice
9   H10 What is your preference of the concierge service?   ['Regular concierge services', 'Online concierge service']  Multiple Choice
10  H11 Consumer Intentions ['']    Multiple Choice

Intenté:

def get_sections(l):
  print("l: ", l, "== ['']", l == [''])
  if l == ['']:
    return "Section"
  else:
    pass

df = df.head()
df.QType = df.apply(lambda row: get_sections(row['Answers']), axis = 1)

Pero me devuelve None:

1    None
2    None
3    None
4    None
5    None
Name: QType, dtype: object

Actualizacion

Tambien intenté:

import numpy as np

s=df.Answers.apply(lambda x : ['']==x)
df['QType']=np.where(s, 'Section', '')

Pero obtengo None en cada celdas.

Y la respuesta de Lucas Damian:

def get_sections(item):
    try:
        for i in item:
            if i == '':
                print(i)
                return "Section"
            else:
                pass
    except Exception as e:
        print(e)

df.apply(lambda row: get_sections(row['Answers']), axis = 1)

Pero no cambia nada.

1 respuesta 1

0

Puedes intentar modificar tu función de está manera?

Se me ocurre que puedes iterar por cada "lista" y preguntar si el valor es ''.

def get_sections(item):
    try:
        for i in item:
            if i == '':
                print(i)
                return "Section"
            else:
                pass
    except Exception as e:
        print(e)

df.apply(lambda row: get_sections(row['Answers']), axis = 1)

la salida correspondiente debería agregar 'Section' en la fila 4 y 10

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.