I have an n-by-m Pandas DataFrame df
defined as follows. (I know this is not the best way to do it. It makes sense for what I'm trying to do in my actual code, but that would be TMI for this post so just take my word that this approach works in my particular scenario.)
>>> df = DataFrame(columns=['col1'])
>>> df.append(Series([None]), ignore_index=True)
>>> df
Empty DataFrame
Columns: [col1]
Index: []
I stored lists in the cells of this DataFrame as follows.
>>> df['column1'][0] = [1.23, 2.34]
>>> df
col1
0 [1, 2]
For some reason, the DataFrame stored this list as a string instead of a list.
>>> df['column1'][0]
'[1.23, 2.34]'
I have 2 questions for you.
- Why does the DataFrame store a list as a string and is there a way around this behavior?
- If not, then is there a Pythonic way to convert this string into a list?
Update
The DataFrame I was using had been saved and loaded from a CSV format. This format, rather than the DataFrame itself, converted the list from a string to a literal.