Tengo una dataframe con preguntas y respuestas:
,Questions,Answers
0,How do you feel about your next vacation after COVID-19?,"[None, 5, [[283973010, [['1'], ['2'], ['3'], ['4'], ['5'], ['6'], ['7'], ['8'], ['9'], ['10']], 0, [""I don't want vacation"", 'I desperately need a vacation']]]]",
1,When do you think your next vacation can start?,"[None, 2, [[1591633300, [['In next 3 months', None, None, None, 0], ['In next 6 months', None, None, None, 0], ['In next 1 year', None, None, None, 0], ['Only once COVID-19 is under control', None, None, None, 0], ['Only once COVID-19 vaccine is developed', None, None, None, 0]], 0, None, None, None, None, None, 0]]]",
2,What are your preferences regarding medical treatment policy (with additional cost)?,"[None, 4, [[1114695321, [[""Doctor's availability in hotel"", None, None, None, 0], ['Ventilator availability in hotel', None, None, None, 0], ['Tie-ups with nearby hospitals', None, None, None, 0], ['Availability of medical rooms with primary first aid care', None, None, None, 0]], 0, None, None, None, None, None, 0]]]",
3,What is your preferences of complementary breakfast?,"[None, 2, [[1502913435, [['Buffet breakfast with social distancing', None, None, None, 0], ['Buffet breakfast replaced with Ala-carte with limited options', None, None, None, 0], ['Breakfast to be delivered in room with limited options (chargeable)', None, None, None, 0], ['Packaged breakfast only', None, None, None, 0]], 0, None, None, None, None, None, 0]]]",
4,What is your preference for a in-hotel grocery shops for the basic necessity items and packaged food?,"[None, 5, [[61960967, [['1'], ['2'], ['3'], ['4'], ['5'], ['6'], ['7'], ['8'], ['9'], ['10']], 0, ['Not preferred', 'Preferred']]]]",
5,Consumer Personality,"[None, 8, None, -2]",
Me gustaria simplificar la columna Answers
, es por decir deshacerme con [None, 5, [[283973010,
y sólo mantener lo que sigue:
0,How do you feel about your next vacation after COVID-19?,"[['1'], ['2'], ['3'], ['4'], ['5'], ['6'], ['7'], ['8'], ['9'], ['10']], 0, [""I don't want vacation"", 'I desperately need a vacation']]"
1,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']]]]]"
2,...
3,What is your preferences of complementary breakfast?,"[['Buffet breakfast with social distancing', None, None, None, 0], ['Buffet breakfast replaced with Ala-carte with limited options', None, None, None, 0], ['Breakfast to be delivered in room with limited options (chargeable)', None, None, None, 0], ['Packaged breakfast only', None, None, None, 0]]",
4,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']], 0, ['Not preferred', 'Preferred']]",
5,Consumer Personality,"[None, 8, None, -2]",
Como pueden ver, la última columna no es como las otras.
Entonces intenté:
for i, row in df.iterrows():
l = row['Answers']
try:
l = literal_eval(l)
l = l[2][0][1:][0]
except:
print("issue w l:", l)
df.at[i, 'Answers'] = l
Pero nunca funciona:
,Unnamed: 0,Questions,Answers,QType
0,1,When do you think your next vacation can start?,"[['In next 3 months', None, None, None, 0], ['In next 6 months', None, None, None, 0], ['In next 1 year', None, None, None, 0], ['Only once COVID-19 is under control', None, None, None, 0], ['Only once COVID-19 vaccine is developed', None, None, None, 0]]",
1,2,What are your preferences regarding medical treatment policy (with additional cost)?,"[[""Doctor's availability in hotel"", None, None, None, 0], ['Ventilator availability in hotel', None, None, None, 0], ['Tie-ups with nearby hospitals', None, None, None, 0], ['Availability of medical rooms with primary first aid care', None, None, None, 0]]",
2,3,What is your preferences of complementary breakfast?,"[['Buffet breakfast with social distancing', None, None, None, 0], ['Buffet breakfast replaced with Ala-carte with limited options', None, None, None, 0], ['Breakfast to be delivered in room with limited options (chargeable)', None, None, None, 0], ['Packaged breakfast only', None, None, None, 0]]",
3,4,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']]",
4,5,Consumer Personality,"[None, 8, None, -2]",
Y me queda None
en ciertos columnas.
,Unnamed: 0,URL,QID,Questions,Answers,QType
0,1,When do you think your next vacation can start?,"[['In next 3 months', None, None, None, 0], ['In next 6 months', None, None, None, 0], ['In next 1 year', None, None, None, 0], ['Only once COVID-19 is under control', None, None, None, 0], ['Only once COVID-19 vaccine is developed', None, None, None, 0]]",
1,2,What are your preferences regarding medical treatment policy (with additional cost)?,"[[""Doctor's availability in hotel"", None, None, None, 0], ['Ventilator availability in hotel', None, None, None, 0], ['Tie-ups with nearby hospitals', None, None, None, 0], ['Availability of medical rooms with primary first aid care', None, None, None, 0]]",
2,3,What is your preferences of complementary breakfast?,"[['Buffet breakfast with social distancing', None, None, None, 0], ['Buffet breakfast replaced with Ala-carte with limited options', None, None, None, 0], ['Breakfast to be delivered in room with limited options (chargeable)', None, None, None, 0], ['Packaged breakfast only', None, None, None, 0]]",
3,4,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']]",
4,5,Consumer Personality,"[None, 8, None, -2]",
Entonces intenté:
>>>df2 = df
>>>for i, row in df2.iterrows():
l = row['Answers']
try:
lis = []
for li in l:
Not_none_values = filter(None.__ne__, li)
lis += list(Not_none_values)
except:
print("issue w l:", l)
df.at[i, 'Answers'] = lis
Pero me devuelve:
df['Answers'].head()
0 [[, [, ', I, n, , n, e, x, t, , 3, , m, o, ...
1 [[, [, ", D, o, c, t, o, r, ', s, , a, v, a, ...
2 [[, [, ', B, u, f, f, e, t, , b, r, e, a, k, ...