首页 > 解决方案 > 如何访问PANDAS python的数组内部数组

问题描述

对于以下数组;[[[11, 22, 33]]],[[[32, 12, 3]]],我想提取第一行,它应该输出 11,22,33。但是,使用以下代码,我得到了 [[11, 22, 33]]。如何删除双括号?

df = pd.DataFrame([
                   [[[11, 22, 33]]], 
                   [[[32, 12, 3]]]
                   ], index=[1, 2], columns=['ColA'])

df[df.index == 1].ColA.item()

预期输出应该是11,22,33的形式;没有括号

标签: pythonpandas

解决方案


.astype(str)andstr.replace与正则表达式运算符 ( |) 一起使用。然后我们iat用来获取第一个值:

df['ColA'].astype(str).str.replace('\[|\]', '').iat[0]

输出

'11, 22, 33'

注意:您的值的类型从更改liststring


或使用本机 python 函数strreplace

str(df['ColA'].iat[0]).replace('[', '').replace(']', '')

推荐阅读