1

I am struggling to simply append the following tables into one:

chr    pos    gene
1    100    A
2    150    B

chr    pos    gene
1    100    A
2    150    F
4    800    D

I would like the final table to look like:

chr    pos    gene    chr    pos    gene
1    100    A    1    100    A
2    150    B    2    150    F
                4    800    D

The tables have same number of columns, but different number of rows. NaN or 0 can be used to fill those empty spots if needed. This is just for us to better visually compare a few table files at once. Nothing fancy needed.

I have tried to use pandas following methods from: https://pandas.pydata.org/pandas-docs/stable/merging.html but the methods here would combine rows with same index - which I do not need. I also tried to just use csv.reader (How two merge several .csv files horizontally with python?), but my output was empty without error message... Not sure if it is because the number of rows were different.

Any advice would be appreciated. Thank you.

1 Answer 1

1

Although it makes this dataframe very hard to use for later processing, you can do this with pd.concat. If your dataframes are called df1 and df2 respectively:

>>> pd.concat((df1,df2), axis=1).replace(np.nan, '')
   chr  pos gene  chr  pos gene
0    1  100    A    1  100    A
1    2  150    B    2  150    F
2                   4  800    D

The replace call is just to get blank strings instead of NaN. As you say in your question, NaNs are acceptable, so you could omit that, and just use:

>>> pd.concat((df1,df2), axis=1)
   chr    pos gene  chr  pos gene
0  1.0  100.0    A    1  100    A
1  2.0  150.0    B    2  150    F
2  NaN    NaN  NaN    4  800    D
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.