我有两个数据帧:
masterdf:
code name
K Transcription
L Replication, recombination and repair
M Cell wall/membrane/envelope biosynthesis
S Function unknown
多年筹资框架:
query code
1 S
2 K
3 MK
4 LS
5 -
我的masterdf
可以说是一个包含两个重要列code和name的数据库。我的mydf
有重要的列代码
现在,我想根据这两个代码列匹配这两个表^因此,{
值得注意的是,masterdf
中的每个字母都有一个记录,但是mydf
中的字母可以由2个以上的字母组成
如果我只有一个字母代码,我就可以做到:
mydf.merge(masterdf,on='code').sort_values("query")
当有字母组合时,我如何获得文本
我的预期输出(此处name
由&
组合而成):
实现这一点的关键是将
mydf
中的code
列扩展到一个列表中,然后explode
将数据帧扩展到基于该列表的更多行中。一旦这样做,就可以合并两个数据帧,然后聚合结果注意,我为
-
添加了一行到masterdf
。如果您是从文件加载数据帧,则应该能够在加载数据帧后添加此项第一步将
'MK'
分成[M, K]
和'LS'
分成[L, S]
。所有其他代码将是一个元素列表输出:
下一步将包含多个代码的行转换为多行,从而允许您在下一步中合并
输出:
合并将从
masterdf
引入name
列输出:
最后一步将展开的行聚合回所需的结果
输出:
相关问题 更多 >
编程相关推荐