Pandas DataFrame存储列表为字符串:如何转换回列表? [英] Pandas DataFrame stored list as string: How to convert back to list?

查看:4213
本文介绍了Pandas DataFrame存储列表为字符串:如何转换回列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 n -by- m Pandas DataFrame df 定义如下。 (我知道这不是最好的方法,对于我在实际代码中要做的事情,这是有道理的,但是这个帖子的TMI是正确的,所以只要我说这个方法适用于我的特定情况。)

 >>> df = DataFrame(columns = ['col1'])
>>> df.append(Series([None]),ignore_index = True)
>>> df
空DataFrame
列:[col1]
索引:[]

我将列表存储在DataFrame的单元格中,如下所示。

 >>> df ['column1'] [0] = [1.23,2.34] 
>>> df
col1
0 [1,2]

由于某些原因, DataFrame将此列表存储为字符串而不是列表。

 >>> df ['column1'] [0] 
'[1.23,2.34]'

有2个问题。


  1. 为什么DataFrame将列表存储为字符串,并且有一种方法来解决此问题?

  2. 如果没有,那么是否有一个Pythonic方式将此字符串转换为列表?






更新



我是DataFrame使用已被保存并从CSV格式加载。 这个格式而不是DataFrame本身将列表从字符串转换为文字。

解决方案

正如你所指出的,这是通常在保存和加载大熊猫DataFrames作为 .csv 文件时发生的,这是一种文本格式。



在这种情况下,这是因为列表对象具有字符串表示形式,允许将它们存储为 .csv 文件。加载 .csv 然后将产生该字符串表示。



如果要存储实际对象,应该你使用 DataFrame.to_pickle()(注意:对象必须可挑选!)。



回答你的第二个问题,您可以使用 ast.literal_eval

 >>>来自ast import literal_eval 
>>>> literal_eval('[1.23,2.34]')
[1.23,2.34]


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.

  1. Why does the DataFrame store a list as a string and is there a way around this behavior?
  2. 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.

解决方案

As you pointed out, this can commonly happen when saving and loading pandas DataFrames as .csv files, which is a text format.

In your case this happened because list objects have a string representation, allowing them to be stored as .csv files. Loading the .csv will then yield that string representation.

If you want to store the actual objects, you should you use DataFrame.to_pickle() (note: objects must be picklable!).

To answer your second question, you can convert it back with ast.literal_eval:

>>> from ast import literal_eval
>>> literal_eval('[1.23, 2.34]')
[1.23, 2.34]

这篇关于Pandas DataFrame存储列表为字符串:如何转换回列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆