Python 迭代包含嵌套数组的数据帧列(重新格式化的请求)
我希望你能帮忙。几周前,您确实在一个关于嵌套数组的类似问题上提供了巨大的帮助。 今天我遇到了类似的问题,我尝试了您在下面的链接中提供的所有解决方案 我的数据是一个包含描述点的ORB向量。它返回一个列表。当我将列表转换为数组时,我得到了这个输出Python 迭代包含嵌套数组的数据帧列(重新格式化的请求),python,pandas,numpy-ndarray,Python,Pandas,Numpy Ndarray,我希望你能帮忙。几周前,您确实在一个关于嵌套数组的类似问题上提供了巨大的帮助。 今天我遇到了类似的问题,我尝试了您在下面的链接中提供的所有解决方案 我的数据是一个包含描述点的ORB向量。它返回一个列表。当我将列表转换为数组时,我得到了这个输出 data=np.asarray([['Test /file0090', np.asarray([[ 84, 55, 189], [248, 100, 18], [ 68, 0,
data=np.asarray([['Test /file0090',
np.asarray([[ 84, 55, 189],
[248, 100, 18],
[ 68, 0, 88]])],
['aa file6565',
np.asarray([[ 86, 58, 189],
[24, 10, 118],
[ 68, 11, 0]])],
['aa filejjhgjgj',
None],
['Test /file0088',
np.asarray([[ 54, 58, 787],
[ 4, 1, 18 ],
[ 8, 1, 0 ]])]])
这是一个小样本,真实数据是一个800.000 x 2的数组
某些图像不返回任何描述符点,且值显示为“无”
下面是一个示例,我刚刚选择了两行,其中的值为“None”
再一次,我需要在nx4中得到这一点(在本例中,我们有4个变量,但我的实际数据有33个变量),这类:
col0, Col1, Col2, col3,
Test /file0090 84, 55, 189
Test /file0090 248, 100, 18
Test /file0090 84, 55, 189
'aa file6565' 86, 58, 189
'aa file6565' 24, 10, 118
'aa file6565' 68, 11, 0
'aa filejjhgjgj' 0 0 0
'Test /file0088 54, 58, 787
'Test /file0088 4, 1, 18
'Test /file0088 8, 1, 0
链接中提供的解决方案的问题是,当它返回的数组中有这个“None”值时
ufunc 'add' did not contain a loop with signature matching types (dtype('<U21'), dtype('<U21')) -> dtype('<U21')
ufunc“add”不包含具有签名匹配类型的循环(dtype(“您可以使用df.fillna(“”)修改@anky answer以处理空值)
:
返回
col0 Col0 Col1 Col2
Test /file0090 84.0 55.0 189.0
Test /file0090 248.0 100.0 18.0
Test /file0090 68.0 0.0 88.0
aa file6565 86.0 58.0 189.0
aa file6565 24.0 10.0 118.0
aa file6565 68.0 11.0 0.0
aa filejjhgjgj 0.0 0.0 0.0
Test /file0088 54.0 58.0 787.0
Test /file0088 4.0 1.0 18.0
Test /file0088 8.0 1.0 0.0
对于无
的行,所需的输出是什么?是否只想删除它们?
df = pd.DataFrame(data).add_prefix('col')
df = df.fillna('').explode('col1').reset_index(drop=True)
df = df.join(pd.DataFrame(df.pop('col1').tolist()).add_prefix('Col')).fillna(0)
col0 Col0 Col1 Col2
Test /file0090 84.0 55.0 189.0
Test /file0090 248.0 100.0 18.0
Test /file0090 68.0 0.0 88.0
aa file6565 86.0 58.0 189.0
aa file6565 24.0 10.0 118.0
aa file6565 68.0 11.0 0.0
aa filejjhgjgj 0.0 0.0 0.0
Test /file0088 54.0 58.0 787.0
Test /file0088 4.0 1.0 18.0
Test /file0088 8.0 1.0 0.0