我对Python/JSON非常陌生,所以请耐心等待。我可以在R中实现这一点,但我们需要使用Python将其转换为Python/Spark/MongoDB。另外,我只是发布了一个最小的子集-我有更多的文件类型,因此如果有人可以帮助我这一点,我可以在此基础上集成更多的文件和文件类型:
回到我的问题上来:
我有两个tsv输入文件需要合并并转换为JSON。这两个文件都有gene和sample列以及一些附加列。然而,gene
和{g3
。类似地,这两个文件在sample
列中有重叠值和非重叠值。在
# f1.tsv – has gene, sample and additional column other1
$ cat f1.tsv
gene sample other1
g1 s1 a1
g1 s2 b1
g1 s3a c1
g2 s4 d1
# f2.tsv – has gene, sample and additional columns other21, other22
$ cat f2.tsv
gene sample other21 other22
g1 s1 a21 a22
g1 s2 b21 b22
g1 s3b c21 c22
g2 s4 d21 d22
g3 s5 f21 f22
基因形成顶层,每个基因有多个样本形成第二层次,附加的列形成第三层次的{other1
,第二个文件有other21
和{other31
和{
如何将两个csv文件转换为基于两个公共列的单级多级别JSON?在
如果能帮上忙,我会非常感激的。在
谢谢!在
分步骤进行:
tsv
文件并将来自不同基因的信息聚合到字典中。在代码如下:
还有一个选择。当你开始添加更多文件时,我试着让它更容易管理。可以在命令行上运行并提供参数,每个要添加的文件都有一个参数。基因/样本名称存储在字典中以提高效率。所需JSON对象的格式化是在每个类的format()方法中完成的。希望这有帮助。在
这看起来是
pandas
的问题!不幸的是,熊猫只带我们走了这么远,然后我们不得不自己做一些操作。这既不是快速的,也不是特别高效的代码,但它可以完成任务。在如果这看起来是一个解决方案,将为您工作,我很高兴花一些时间清理它,使其诱饵更加可读和高效。在
PS:如果你喜欢R,那么pandas就是最好的选择(它是为了给python中的数据提供一个类似R的接口)
相关问题 更多 >
编程相关推荐