在大 pandas /蟒蛇,读阵列存储为字符串 [英] In pandas/python, reading array stored as string
问题描述
我有一个熊猫数据框,其中的一列具有字符串作为每个元素的数组。
所以,这样的事情。
COL1 COL2
0 120'ABC','高清']
1 130'GHI','荷航']
现在,当我使用这个它to_csv似乎罚款保存为csv。
当我使用from_csv读回我似乎读回。
但后来当我在每个单元格分析值数组
'['''''A''B''C'等。
所以基本上它不是阅读它作为一个数组,但一组字符串。有人可以建议我怎么能这个字符串转换为数组?
我的意思是说数组已存储就像一个字符串
'[\\ABC \\',\\'高清\\']'
由于在其他问题中提到,你应该使用 literal_eval
这里:
从AST进口literal_eval
DF ['COL2'] = DF ['COL2']。应用(literal_eval)
在行动:
在[11]:DF = pd.DataFrame([120,'[\\ABC \\',\\'高清\\']'],[130,'[\\ GHI \\',\\'荷航\\']'],列='A','B'])[12]:DF
出[12]:
A B
0 120'ABC','高清']
1 130'GHI','荷航']在[13]:df.loc [0,B]#字符串
出[13]:['ABC','高清']在[14]:df.B = df.B.apply(literal_eval)在[15]:df.loc [0,B]#现在它的一个列表
出[15]:['ABC','高清']
I have a pandas dataframe where one of the columns has array of strings as each element.
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
'[' ''' '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\']'
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 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']
这篇关于在大 pandas /蟒蛇,读阵列存储为字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!