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
NaN
en los espacios que quieresNone
, es tan fácil comodf.fillna(None)
, no sé si eso te sirva.