0

To understand the different python data structures and their conversion, I came across a data frame which is displayed below:

enter image description here

The columns refer to ratings of the top 5 from each category i.e. Attraction , Hotel etc.

Here when I access the Attraction_Rating value for the first row in the dataframe, I get a string '[4.5, 4.5,4.5,4.5,5.0]'.

I wanted to understand how can we convert this string back to a list which contains these floats values without using regex. float(s) where s is a string, is clearly not applicable here and I couldn't think of any other simpler solution.

Can anyone please tell me how to do that or point me in the right direction here.

My output should be [4.5, 4.5,4.5,4.5,5.0] i.e. list of floats.

I'll really appreciate any help on this

  • You can use `eval`, but you probably shouldn't. Why do you have a bunch of strings *representing python literals of lists of floats in the first place?* – juanpa.arrivillaga Feb 24 '19 at 18:48
  • Try this: df['Attraction_Rating'][0] Does it gives back the first value of the list, or the list itself? – Kristóf Varga Feb 24 '19 at 18:49
  • The dataframe was like that. Any suggestion? – Shekhar Tanwar Feb 24 '19 at 18:49
  • "the dataframe was like that" Um, dataframes don't just appear in your computer's main memory. *What made it like that*? Again, you can use `eval`, but the problem is that you have that string in the first place. You can also parse the string by splitting it, or various other ways (including regex, why *not* use regex?). – juanpa.arrivillaga Feb 24 '19 at 18:51
  • Yeah it doesn't. I've tried everything, plus I fear this will be closed by the moderators :D – Shekhar Tanwar Feb 24 '19 at 18:53
  • Fundamentally, this means somewhere in this project someone has decided to dump a *string representation of a python list of floats* to a text file and call that serialization. That is the wrong way to do things. – juanpa.arrivillaga Feb 24 '19 at 18:56
  • Yes, it is. however I believe coming across such problems and finding a solution would be a good programming exercise. The below solution works very well. – Shekhar Tanwar Feb 24 '19 at 18:58
  • @ShekharTanwar why didn't you want to use regex? That would also be a good solution. – juanpa.arrivillaga Feb 24 '19 at 18:59
  • I'm still in the beginning stages of Python, and regex was something I had planned to learn after I master the data structures part. Thanks everyone for the support. – Shekhar Tanwar Feb 24 '19 at 19:01
  • @juanpa.arrivillaga, if you have read the question correct, how do you think it's a duplicate? I'm not sure why you had to mark it as duplicate when clearly this was a different problem statement. – Shekhar Tanwar Feb 24 '19 at 21:54
  • You are asking how to turn a string representation of a list to that list. This is what that duplicate addresses. – juanpa.arrivillaga Feb 24 '19 at 21:59
  • I believe it's not. The link you shared , has the elements as strings the list, while here the quotes to denote a string value, are present outside the list itself. Also, I first referred the link you had shared, and couldn't find the answer. Anyway, I got the answer I needed. – Shekhar Tanwar Feb 24 '19 at 23:50

1 Answers1

1

This should work for a string like that.

data_string = '[4.5, 5.5, 6.5, 7.5]'

def convert(string):
  return list(map(lambda v: float(v), string[1:-1].split(',')))

print(convert(data_string))