0

Tengo un dataframe y para cada línea en la coumna QType, si está marcada como section, quiero poner el contenido de la columna de preguntas en la (nueva) columna Section, hasta la próxima vez que encuentre section en QType. Aqui esta la dataframe:

>>> df[['Questions','QType']].head(10)
                                           Questions            QType
0    When do you think your next vacation can start?  Multiple Choice
1  What are your preferences regarding medical tr...  Multiple Choice
2  What is your preferences of complementary brea...  Multiple Choice
3  What is your preference for a in-hotel grocery...     Likert Scale
4                               Consumer Personality          Section
5         What is your preference of hotel check-in?  Multiple Choice
6  What is your preference of payment during Chec...  Multiple Choice
7  What is your preference of hotel cancellation ...  Multiple Choice
8       Boooooooooooooooooooooo                       Multiple Choice
9  What is your preference of the concierge service?  Multiple Choice

Y me gustaria una cosa como:

>>> df[['Questions','QType']].head(10)
                                           Questions            QType    Section
0    When do you think your next vacation can start?  Multiple Choice    None
1  What are your preferences regarding medical tr...  Multiple Choice    None
2  What is your preferences of complementary brea...  Multiple Choice    None
3  What is your preference for a in-hotel grocery...     Likert Scale    None
5         What is your preference of hotel check-in?  Multiple Choice    Consumer Personality
6  What is your preference of payment during Chec...  Multiple Choice    Consumer Personality
7  What is your preference of hotel cancellation ...  Multiple Choice    Consumer Personality
9  What is your preference of the concierge service?  Multiple Choice    Booooooooooooooooooooo

Hasta hoy intenté:

>>> section = None
>>> for i, row in df.iterrows():
...     if df.loc[i, 'QType'] == 'Section':
...         section = df.loc[i,'Questions']
...     df.loc[i,'Section']=section
...

Y obtengo:

>>> df[['Questions','QType','Section']].head(10)
                                           Questions            QType               Section
0    When do you think your next vacation can start?  Multiple Choice                   NaN
1  What are your preferences regarding medical tr...  Multiple Choice                   NaN
2  What is your preferences of complementary brea...  Multiple Choice                   NaN
3  What is your preference for a in-hotel grocery...     Likert Scale                   NaN
4                               Consumer Personality          Section  Consumer Personality
5         What is your preference of hotel check-in?  Multiple Choice  Consumer Personality
6  What is your preference of payment during Chec...  Multiple Choice  Consumer Personality
7  What is your preference of hotel cancellation ...  Multiple Choice  Consumer Personality
8       What is your preference of the guest policy?  Multiple Choice  Consumer Personality

Solo necesito deshacerme con las lineas que tienen en 'Questions' el nombre de la 'Section' como la liea 5

1
  • Si lo que haces te da en NaN en los espacios que quieres None, es tan fácil como df.fillna(None), no sé si eso te sirva.
    – user166844
    Commented el 3 jul. 2020 a las 15:50

1 respuesta 1

0

Para eliminar aquellas filas donde el valor de Questions es igual al valor de Section, puedes hacer lo siguiente:

# Obtener los índices de las filas a eliminar
i = df[df["Questions"] == df["Section"]].index

# Eliminar las filas
df.drop(index=i, inplace=True)

# Restablecer los índices del dataframe para
# que sean números consecutivos a partir de 0
df.reset_index(drop=True, inplace=True)

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.