在 pandas/python 中,读取存储为字符串的数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23119472/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 21:56:12  来源:igfitidea点击:

In pandas/python, reading array stored as string

pythonarrayscsvpandasdataframe

提问by AMM

I have a pandas dataframe where one of the columns has array of strings as each element.

我有一个 Pandas 数据框,其中一列的每个元素都有字符串数组。

So something like this.

所以像这样的事情。

  col1 col2
0 120  ['abc', 'def']
1 130  ['ghi', 'klm']

Now when i store this to csv using to_csv it seems fine. When i read it back using from_csv i seems to read back. But then when i analyse the value in each cell the array is

现在,当我使用 to_csv 将它存储到 csv 时,它看起来很好。当我使用 from_csv 读回它时,我似乎读回了。但是当我分析每个单元格中的值时,数组是

'[' ''' 'a' 'b' 'c' and so on. So essentially its not reading it as an array but a set of strings. Can somebody suggest how I can convert this string into an array?

'[' ''' 'a' 'b' 'c' 等等。所以本质上它不是将其作为数组读取,而是将其读取为一组字符串。有人可以建议我如何将此字符串转换为数组吗?

I mean to say the array has been stored like a string

我的意思是说数组已像字符串一样存储

'[\'abc\',\'def\']'

回答by Andy Hayden

As mentioned in the other questions, you should use literal_evalhere:

正如其他问题中提到的,你应该literal_eval在这里使用:

from ast import literal_eval
df['col2'] = df['col2'].apply(literal_eval)

In action:

在行动:

In [11]: df = pd.DataFrame([[120, '[\'abc\',\'def\']'], [130, '[\'ghi\',\'klm\']']], columns=['A', 'B'])

In [12]: df
Out[12]:
     A              B
0  120  ['abc','def']
1  130  ['ghi','klm']

In [13]: df.loc[0, 'B'] ?# a string
Out[13]: "['abc','def']"

In [14]: df.B = df.B.apply(literal_eval)

In [15]: df.loc[0, 'B']  #?now it's a list
Out[15]: ['abc', 'def']

回答by AMM

Nevermind got it.

没关系得到它。

All i had to do was

我所要做的就是

arr = s[1:-1].split(',')

This got rid of the square brackets and also split the string into an array like I wanted.

这摆脱了方括号,并将字符串拆分为我想要的数组。

回答by shaktimaan

Without pandas, this is one way to do it using the astmodules' literal_eval():

没有Pandas,这是使用ast模块的一种方法literal_eval()

>>> data = "['abc', 'def']"
>>> import ast
>>> a_list = ast.literal_eval(data)
>>> type(a_list)
<class 'list'>
>>> a_list[0]
'abc'

回答by Alex S

Maybe try using a different separator value? Like so:

也许尝试使用不同的分隔符值?像这样:

DataFrame.to_csv(filepath, sep=';')

and then read with

然后阅读

DataFrame.from_csv(filepath, sep=';')