Tengo una dataframe y me gustaria saber si el contenido en las celdas es una lista o no y deshacerme de ello si es el caso:
>>>data3[data3.QType=='Multiple Choice'].iloc[16:19,3:13]
Questions Answers Section QType Theme Topics Answer0 Answer1 Answer2 Answer3
17 What is you preference of room/suites with bal... ['Preferred', 'Not preferred'] Consumer Intentions Multiple Choice Hotel ABC ['prefer', 'room', 'suit', 'balconi'] Preferred Not preferred NaN NaN
18 How do you want your guestroom to look like? ['Contemporary style guestrooms', ['18lKwM8N2E... Consumer Intentions Multiple Choice Hotel ABC ['want', 'guestroom', 'look', 'like'] Contemporary style guestrooms ['18lKwM8N2EuQujkFj81pcSrt5bjy9wppKC8mjYQZp0L0... Traditional style guestrooms ['1OL5AUAqisiQBDrkNM0OQ0_NcHxmisM0y8I6OJpRcSdI...
19 How do you want to know about our recent offer... ['Personalized emails', 'Text messages', 'Web ... Media Consumption Multiple Choice Hotel ABC ['want', 'know', 'recent', 'offer'] Personalized emails Text messages Web Blogs Paper advertisements
Intenté:
import numpy as np
import ast
def get_rid_of_images(cell):
try:
question = ast.literal_eval(cell)
question_type = type(question)
if question_type == list:
return None
else: return cell
except SyntaxError:
return cell
except ValueError:
return cell
import ast
data3 = data2.copy()
columns = [x for x in data3.iloc[:,9:].columns]
for column in columns:
data3.loc[data3.QType=='Multiple Choice',column].iloc[:20,3:25] = data3[data3.QType=='Multiple Choice'].apply(get_rid_of_images)
Pero no cambia nada
Al menos para ver si funciona, pero el mensaje que confirmaría que nunca aparece.
Actualizacion
Intenté la respuesta de Pablo Prieto
import ast
def remove_list(cell):
try:
value = ast.literal_eval(cell)
if type(value) == list:
return None
else:
return cell
except SyntaxError:
return cell
except ValueError:
return cell
columns = [x for x in data3.iloc[:,9:].columns]
data3[columns] = data3[columns].applymap(remove_list)
El resultado es
Section QType Theme Topics Answer0 Answer1 Answer2 Answer3 Answer4 Answer5
17 Consumer Intentions Multiple Choice Hotel ABC ['prefer', 'room', 'suit', 'balconi'] Preferred Not preferred NaN NaN NaN NaN
18 Consumer Intentions Multiple Choice Hotel ABC ['want', 'guestroom', 'look', 'like'] Contemporary style guestrooms None Traditional style guestrooms None Classical style guestrooms None
19 Media Consumption Multiple Choice Hotel ABC ['want', 'know', 'recent', 'offer'] Personalized emails Text messages Web Blogs Paper advertisements Video advertisements Advertisements on social media
Sin embargo, ¿sabría usted, en la función y cuando hay listas, cómo mover las respuestas que están en el lado derecho de las listas? Para obtener una cosa como:
Section QType Theme Topics Answer0 Answer1 Answer2 Answer3 Answer4 Answer5
17 Consumer Intentions Multiple Choice Hotel ABC ['prefer', 'room', 'suit', 'balconi'] Preferred Not preferred NaN NaN NaN NaN
18 Consumer Intentions Multiple Choice Hotel ABC ['want', 'guestroom', 'look', 'like'] Contemporary style guestrooms Traditional style guestrooms Classical style guestrooms None