-
-
Save csharpforevermore/557bc9db2dc35299e1e17d9793778099 to your computer and use it in GitHub Desktop.
Join multiple pandas dataframes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import glob | |
import sys | |
import pandas | |
def read_filenames(names): | |
for arg in names: | |
if "*" in arg: | |
for file in glob.glob(arg): | |
yield file | |
else: | |
yield arg | |
data = [] | |
for sample in read_filenames(sys.argv[1:]): | |
frame = pandas.read_csv( | |
sample, | |
sep="\t", | |
header=None, | |
names=["chr", "strand", "coord", sample], | |
index_col=[0, 1, 2] | |
) | |
data.append(frame) | |
data_joined = data[0].join(data[1:], how='outer') | |
sum = data_joined[data_joined.columns].sum(axis=1) | |
data_joined.insert(0, "scoreSum", sum) | |
print(data_joined.to_csv( | |
sep="\t", | |
na_rep=0, | |
float_format='%.0f' | |
)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment