Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/328.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python str将数组封装到带有熊猫的数组中_Python_Arrays_String_Pandas - Fatal编程技术网

Python str将数组封装到带有熊猫的数组中

Python str将数组封装到带有熊猫的数组中,python,arrays,string,pandas,Python,Arrays,String,Pandas,我正在使用一个包含数组的数据帧。在读取_cvs时,熊猫似乎正在str模式下存储我的审查员。像这样: df['column'].iloc[3] >>>'[50.6402809, 4.6667145]' type(df['column'].iloc[3]) >>> str 如何将整个列转换为数组?像这样: df['column'].iloc[3] >>>[50.6402809, 4.6667145] type(df['column'].il

我正在使用一个包含数组的数据帧。在
读取_cvs
时,熊猫似乎正在
str
模式下存储我的审查员。像这样:

df['column'].iloc[3]
>>>'[50.6402809, 4.6667145]'

type(df['column'].iloc[3])
>>> str
如何将整个列转换为数组?像这样:

df['column'].iloc[3]
>>>[50.6402809, 4.6667145]

type(df['column'].iloc[3])
>>> array

如果需要numpy阵列,请使用lambda函数并将其转换为阵列:

import ast

df['column'] = df['column'].apply(lambda x: np.array(ast.literal_eval(x)))
如有需要,请列出:

df['column'] = df['column'].apply(ast.literal_eval)

df['column'] = [ast.literal_eval(x) for x in df['column']]

您可以使用
ast
模块逐字解释字符串。然而,这可能是危险的,尤其是当从文件中读取数据时,或者更糟的是,在线读取数据时

另一种方法是直接使用函数解析文件:

In [19]: parsed = (
    ...:     df['column']
    ...:     .str.strip('[]')
    ...:     .str.split(', ', )
    ...:     .apply(lambda x: np.array(x).astype(float)))
    ...:

In [20]: parsed
Out[20]:
0    [0.45482146988492345, 0.40132331304489344]
1      [0.4820128044982769, 0.6930103661982894]
2      [0.15845986027370507, 0.825879918750825]
3      [0.08389109330674027, 0.031864037778777]
Name: column, dtype: object
可能重复的