python - 如何将熊猫中的一串数字转换为整数
问题描述
我正在尝试找到一种将边界框坐标字符串转换为整数列表的方法。我的每个想法都会引发 KeyError,有什么提示吗?
for row in bounding_image_df['bounding_box']:
bounding_image_df['bounding_box'][row] = list(map(int, bounding_image_df['bounding_box'][row].split(' ')))
for row in bounding_image_df['bounding_box']:
bounding_image_df['bounding_box'][row] = bounding_image_df['bounding_box'][row].split(' ')
for row in bounding_image_df['bounding_box']:
pd.to_numeric(bounding_image_df['bounding_box'][row], errors='ignore')
这些都抛出相同的错误,一个关键错误,唯一的描述是数据帧第一行中未转换的字符串。
KeyError: '60 127 253 72'
解决方案
I've understood you have a dataframe like this:
dicc = {'colum1': ['row1', 'row2', 'row3'],
'bounding_box': ['60 127 253 72','55 137 243 22','56 227 113 78']
}
df = pd.DataFrame(dicc)
df
Then, if you want to convert the column 'bounding box' to int, I would use:
df['bounding_box']=df['bounding_box'].str.split(' ').apply(lambda x: [int(num) for num in x])
Then if you print(df), you'll get:
colum1 bounding_box
0 row1 [60, 127, 253, 72]
1 row2 [55, 137, 243, 22]
2 row3 [56, 227, 113, 78]
推荐阅读
- java - 使用 Apache Beam 将 SAP HANA 连接到 Big Query
- c++ - C ++中的指针输出问题
- sql - 如何使用 case 语句在 PIVOT 中获取多个列
- ansible - Ansible:你可以在另一个变量中迭代一个变量吗?
- angular - ngif 哪个函数导致 ExpressionChangedAfterItHasBeenCheckedError
- java - 在没有管理员权限的情况下将文件复制到 Windows 中的 C:/Program Files
- node.js - Node Js高频文件写入
- dart - 标志忽略属性 build_runner 的序列化
- r - 在 for 循环中找不到对象?
- odoo - Odoo pos 不适用于管理员以外的用户