This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Concatenating text files based on common indices

I have two text-files containing the abundances of genes in samples. However, one of the files measures the abundance of a greater variety of genes than the other, and therefore cannot be completely concatenated. Thus, I'm trying to concatenate the lines from the larger file that share an identical gene index as lines from the smaller file, such that:

  >df1
                Sample1 Sample2 Sample3  
   Gene1    0.001       0.002      0.003
   Gene2    0.001       0.002      0.003  
   Gene3    0.001       0.002      0.003 


  >df2
                Sample4 Sample5 Sample6  
   Gene1    0.001       0.002      0.003
   Gene1.1 0.001       0.002      0.003
   Gene2    0.001       0.002      0.003
   Gene2.1 0.001       0.002      0.003
   Gene3    0.001       0.002      0.003 


    >df1and2
                Sample1 Sample2 Sample3 Sample4 Sample5 Sample6
   Gene1    0.001       0.002      0.003.   0.001       0.002      0.003
   Gene2    0.001       0.002      0.003    0.001       0.002      0.003
   Gene3    0.001       0.002      0.003    0.001       0.002      0.003

Suggestions in Python or Bash are both welcome. Thank you!

bash python

I've removed tags such as genetics, genes and bioinformatics. The last tag makes no sense - EVERY QUESTION here is related to bioinformatics.

1 answer

In general this is called an inner join, which is easy using the pandas library in Python.

import pandas as pd

df1and2 = df1.merge(df2, how='inner', left_index=True, right_index=True)

Thanks for your response! However, when I tried it with my text files, I get an error stating: AttributeError: 'str' object has no attribute 'merge', even though they are in a very similar format to my example. What could be causing this problem?

'str' object has no attribute 'merge'

It appears you are trying to merge file names rather than dataframes. You have to read in those files first such that dataframes are named df1 and df2 and then it should work.

Log in to answer this question.