在 pandas/python 中,读取存储为字符串的数组 [英] In pandas/python, reading array stored as string
问题描述
我有一个 Pandas 数据框,其中一列的每个元素都有字符串数组.
I have a pandas dataframe where one of the columns has array of strings as each element.
像这样的事情.
col1 col2
0 120 ['abc', 'def']
1 130 ['ghi', 'klm']
现在,当我使用 to_csv 将它存储到 csv 时,它看起来很好.当我使用 from_csv 读回它时,我似乎读回了.但是当我分析每个单元格中的值时,数组是
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
'[' ''' 'a' 'b' 'c' 等等.所以本质上它不是将其作为数组读取,而是将其读取为一组字符串.有人可以建议我如何将此字符串转换为数组吗?
'[' ''' '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?
我的意思是说数组已经像字符串一样存储
I mean to say the array has been stored like a string
'[\'abc\',\'def\']'
推荐答案
正如在其他问题中提到的,你应该在这里使用 literal_eval
:
As mentioned in the other questions, you should use literal_eval
here:
from ast import literal_eval
df['col2'] = df['col2'].apply(literal_eval)
在行动:
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']
这篇关于在 pandas/python 中,读取存储为字符串的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!