python - 如何访问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的形式;没有括号
解决方案
将.astype(str)
andstr.replace
与正则表达式或运算符 ( |
) 一起使用。然后我们iat
用来获取第一个值:
df['ColA'].astype(str).str.replace('\[|\]', '').iat[0]
输出
'11, 22, 33'
注意:您的值的类型从更改list
为string
或使用本机 python 函数str
和replace
:
str(df['ColA'].iat[0]).replace('[', '').replace(']', '')
推荐阅读
- selenium-webdriver - 如何使用 Selenium/Webdriver 自动化聊天框问答?
- c - 如何根据分隔符将数组存储在三个不同的数组中
- python - 如何获得由开始和结束日期时间数组定义的熊猫数据框范围的平均值?
- spring-boot - Spring Boot - 带有 Spring Security 的 Hazelcast 会话复制
- java - 无法使用 java 配置访问属性文件中的所有属性?
- python - 在 tf.keras.Model 中访问训练操作
- php - 如何使用 LDAP php 在组中添加用户?
- javascript - 在 Vue FullCalendar SFC 中拖动外部事件
- c# - 使用条件求和查询然后将结果放入文本框中
- python - NOT NULL 约束失败:users_userprofile.user_id