I'm working on a project in pandas on python. I receive as input a .csv file like this:
Name,Time,Data A,5.6,"[1,2,3]" A,1.2,"[1.4,3,8.9]" ... B,3.4,"[0.2,3,5.1]" ecc..
(I have thousand of datas for everyname and like 10 names). So in pandas the table is:
Name Time Data A 5.6 [1,2,3] A 1.2 [1.4,3,8.9] ... B 3.4 [0.2,3,5.1] ...
I need to convert to another measure unit all the numbers of the list in the "Data" column (so basically, mutiply every number of the list by a scalar). I'm having problems because, in the csv that i receive, the datas are saved as strings. So firstly i've to convert the string to a list of floats, then multiply the 3 numbers in the list for a scalar (e.g. 2) and then convert again the list into a string.
I know that performing an operation on a whole column is like:
df['Data'] = df['Data'].apply(lambda x: x*2)
i can multiply every number of a list "a" in this way:
[x*2 for x in a]
and i can convert the string into a list with ast:
a = ast.literal_eval(a) # (and with a = str(a).strip('[]') i can return to the string)
but i can't combine these 3 things.
Do you have any solution? (not necessarily with the same methods i tried up here). Thank you in advance!