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

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

问题描述

我有一个 n-by-m Pandas DataFrame df 定义如下.(我知道这不是最好的方法.这对于我在实际代码中尝试做的事情是有意义的,但这将是这篇文章的 TMI,所以请相信我的话,这种方法适用于我的特定场景.)

<预><代码>>>>df = DataFrame(columns=['col1'])>>>df.append(Series([None]), ignore_index=True)>>>df空数据帧列:[col1]指数: []

我在这个 DataFrame 的单元格中存储了如下列表.

<预><代码>>>>df['column1'][0] = [1.23, 2.34]>>>df第 1 列0 [1, 2]

出于某种原因,DataFrame 将此列表存储为字符串而不是列表.

<预><代码>>>>df['column1'][0]'[1.23, 2.34]'

我有两个问题要问你.

  1. 为什么 DataFrame 将列表存储为字符串,有没有办法绕过这种行为?
  2. 如果没有,那么是否有一种 Pythonic 的方法可以将此字符串转换为列表?

<小时>

更新

我使用的 DataFrame 已从 CSV 格式保存和加载.这种格式,而不是 DataFrame 本身,将列表从字符串转换为文字.

解决方案

正如您所指出的,当将 Pandas DataFrames 保存和加载为 .csv 文件时,这通常会发生,这是一种文本格式.

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

如果你想存储实际的对象,你应该使用DataFrame.to_pickle()(注意:对象必须是可以picklable的!)

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

<预><代码>>>>从 ast 导入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 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天全站免登陆