我已经使用下面的代码生成了一个数据帧,其中包含所有可能的两种心电图(ECG)导联组合,使用itertools
source = [ 'I-s', 'II-s', 'III-s', 'aVR-s', 'aVL-s', 'aVF-s', 'V1-s', 'V2-s', 'V3-s', 'V4-s', 'V5-s', 'V6-s', 'V1Long-s', 'IILong-s', 'V5Long-s', 'Information-s' ]
target = [ 'I-t', 'II-t', 'III-t', 'aVR-t', 'aVL-t', 'aVF-t', 'V1-t', 'V2-t', 'V3-t', 'V4-t', 'V5-t', 'V6-t', 'V1Long-t', 'IILong-t', 'V5Long-t', 'Information-t' ]
from itertools import product
test = pd.DataFrame(list(product(source, target)), columns=['source', 'target'])
测试数据帧包含256行/行,其中包含所有两种可能的组合
每个组合的值为零,如下所示
test['value'] = 0
测试df如下所示:
我有另一个名为diagramDF
的数据帧,它包含value
列为非零的组合。diagramDF
明显小于test
数据帧
source target value
0 I-s II-t 137
1 II-s I-t 3
2 II-s III-t 81
3 II-s IILong-t 13
4 II-s V1-t 21
5 III-s II-t 3
6 III-s aVF-t 19
7 IILong-s II-t 13
8 IILong-s V1Long-t 353
9 V1-s aVL-t 11
10 V1Long-s IILong-t 175
11 V1Long-s V3-t 4
12 V1Long-s aVF-t 4
13 V2-s V3-t 8
14 V3-s V2-t 6
15 V3-s V6-t 2
16 V5-s aVR-t 5
17 V6-s III-t 4
18 aVF-s III-t 79
19 aVF-s V1Long-t 235
20 aVL-s I-t 1
21 aVL-s aVF-t 16
22 aVR-s aVL-t 1
请注意,前两列source
和target
具有相同的符号
我尝试使用merge
将test
数据帧的零值替换为diagramDF
的非零值,如下所示:
df = pd.merge(test, diagramDF, how='left', on=['source', 'target'])
但是,我收到一个错误,通知我:
ValueError: The column label 'source' is not unique. For a multi-index, the label must be a tuple with elements corresponding to each level
我有什么地方做错了吗?有没有更有效、更快捷的方法
可能会有帮助,
pd.merge(test, diagramDF, how='left', on=['source', 'target'],right_index=True,left_index=True)
选中此项:
相关问题 更多 >
编程相关推荐