2017-08-26 4 views
0

私はコーディングが新しく、扱う大きなデータがたくさんあります。現在、私は26個のtsvファイルをマージしようとしています(それぞれは、一方が他方のカウントであるcontig _numberで、ヘッダなしで2つの列があります。あまりにも多くのメモリを使用するpandas '外側'の複数のcsvsのマージ

tsvは、特定のcontig_numberのカウントを持っていなかった場合は、それがあることはありません。私は最初のテストを実行するためにサブセット化したが、私はスクリプトを実行するときに成功しました。実際のデータは大きい(〜40,000行、2列)、より多くのメモリが使用されます...

サーバ上で500GbのRAMを使用し、それを1日と呼びました。

これはサブセットCSVを上成功しているコードです:

files = glob.glob('*_count.tsv') 
data_frames = [] 
logging.info("Reading in sample files and adding to list") 
for fp in files: 
    # read in the files and put them into dataframes 
    df = pd.read_csv(fp, sep = '\t', header = None, index_col = 0) 
    # rename the columns so we know what file they came from 
    df = df.rename(columns = {1:str(fp)}).reset_index() 
    df = df.rename(columns = {0:"contig"}) 
    # append the dataframes to a list 
    data_frames.append(df) 

logging.info("Merging the tables on contig, and fill in samples with no counts for contigs") 

# merge the tables on gene_id and select how = 'outer' which will include all rows but will leave empty space where there is no data 
df=reduce(lambda left,right: pd.merge(left, right, how='outer', on="contig"), data_frames) 

# this bit is important to fill missing data with a 0 
df.fillna(0, inplace = True) 

logging.info("Writing concatenated count table to file") 

# write the dataframe to file 
df.to_csv("combined_bamm_filter_count_file.tsv", 
        sep='\t', index=False, header=True) 

私が何かアドバイスや提案をいただければ幸いです!たぶんあまりにも多くのメモリを保持するだけで、私は別の何かをしようとする必要があります。

ありがとうございました!

答えて

0

私は通常、pd.concatでこれらの操作を行います。なぜそれがより効率的であるかの正確な詳細はわかりませんが、パンダはインデックスを組み合わせるためにいくつかの最適化を行っています。

私は、あなたがしたい場合は

for fp in files: 
    # read in the files and put them into dataframes 
    df = pd.read_csv(fp, sep = '\t', header = None, index_col = 0) 
    # rename the columns so we know what file they came from 
    df = df.rename(columns = {1:str(fp)}) 
    #just keep the contig as the index 
    data_frames.append(df) 

df_full=pd.concat(data_frames,axis=1) 

、その後df_full=df_full.fillna(0)を行うだろう。

実際、各ファイルには1つの列(インデックス)しかないので、DataFrameの代わりにSeriesと扱うと、より良い結果が得られます。

+0

こんにちはビクター、ありがとうございます! pd.concatを試行する過程で、ファイルのいくつかが誤って解析され、重複したインデックスがあることに気付きました。私はそれを修正し、concat正しい道を与える! – Caitlin

関連する問題