簡體   English   中英

Python Pandas - 比較 2 個數據幀,多個參數

[英]Python Pandas - Compare 2 dataframes, multiple parameters

我有兩張桌子。 一個(下面的 df)大約有 18,000 行,另一個(下面的 mapfile)大約有 800,000 行。 我需要一個可以處理如此大的 DataFrame 的解決方案。

這是一個玩具示例:表 1 - df

Sample    Chr    Start     End    Value
S1        1       100      200     1
S1        2       200      250     1
S2        1       50        75     5
S2        2       150      225     4

表 2 - 映射文件

Name    Chr    Position
P1       1      105
P2       1      60
P3       1      500
P4       2      25
P5       2      220
P6       2      240

我正在嘗試執行以下操作(我的語法錯誤,但我認為這個想法會出現):

for mapline in mapfile:
    for dfline in df:
       if df[dfline]['Chr'] == mapfile[mapline]['Chr']
           if mapfile[mapline]['Position'] > df[dfline]['Start'] & mapfile[mapline]['Position'] < df[dfline]['End']
                  newdf[['Name','Chr','Position','Value', 'Sample']] = pd.DataFrame([ mapfile[mapline]['Name'], mapfile[mapline]['Chr'], mapfile[mapline]['Position'], df[dfline]['Value'], df[dfline]['Sample'] ] )

換句話說:我需要查看 mapfile 中的每個項目(行),看看它的位置是否在 df 中每個 CHR 的任何 START 和 END 之間。 如果是,我需要將它添加到包含兩個表中的 Name、Chr、Position、Sample 和 Value 字段的新文件中。

玩具數據輸出表:

Name    Chr    Position    Value   Sample
P1       1      105         1       S1
P2       1      60          5       S2
P5       2      220         1       S1
P5       2      220         4       S2
P6       2      240         1       S1

到目前為止:我已經得到了上面的內容,並且在找出語法以在 python 中執行一般循環時遇到了問題。 但是,我的理解是,使用諸如 pandas 或 NumPy 之類的包可能會容易得多? 請幫助我找到最有效的方法來做到這一點,並且在此過程中對語法提供一些幫助會很棒。

我嘗試過但無法完成的一些相關帖子使用 Pandas 循環遍歷數據幀的最有效方法是什么? 如何遍歷 Pandas 中 DataFrame 中的行? 將列附加到熊貓數據框根據熊貓中的另一個列值有條件地填充列值

IIUC 您可以使用read_csvmerge

import pandas as pd
import io

temp1=u"""Sample;Chr;Start;End;Value
S1;1;100;200;1
S1;2;200;250;1
S2;1;50;75;5
S2;2;150;225;4"""
#after testing replace io.StringIO(temp1) to filename
dfline = pd.read_csv(io.StringIO(temp1), sep=";")

temp2=u"""Name;Chr;Position
P1;1;105
P2;1;60
P3;1;500
P4;2;25
P5;2;220
P6;2;240"""
#after testing replace io.StringIO(temp2) to filename
mapfile = pd.read_csv(io.StringIO(temp2), sep=";")
print dfline
  Sample  Chr  Start  End  Value
0     S1    1    100  200      1
1     S1    2    200  250      1
2     S2    1     50   75      5
3     S2    2    150  225      4
print mapfile
  Name  Chr  Position
0   P1    1       105
1   P2    1        60
2   P3    1       500
3   P4    2        25
4   P5    2       220
5   P6    2       240

#merge by column Chr
df = pd.merge(dfline, mapfile, on=['Chr'])

#select by conditions
df = df[(df.Position > df.Start) & (df.Position < df.End)]

#subset of df
df =  df[['Name','Chr','Position','Value', 'Sample']]
print df
   Name  Chr  Position  Value Sample
0    P1    1       105      1     S1
4    P2    1        60      5     S2
7    P5    2       220      1     S1
8    P6    2       240      1     S1
10   P5    2       220      4     S2

#if you need reset index
print df.reset_index(drop=True)
  Name  Chr  Position  Value Sample
0   P1    1       105      1     S1
1   P2    1        60      5     S2
2   P5    2       220      1     S1
3   P6    2       240      1     S1
4   P5    2       220      4     S2

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM