簡體   English   中英

Python Pandas使用索引或列標識符連接/合並DataFrame

[英]Python Pandas concatenate/merge DataFrames using index or column identifiers

我想使用'pandas.concat'方法合並兩個DataFrame,但是我不完全理解所有'pandas.concat'參數。 我有兩個DataFrame,在各列中具有相同的標識變量,但在同一列中有所不同。

import pandas as pd
dict_data = {'Treatment': ['C', 'C', 'C'], 'Biorep': ['A', 'A', 'A'], 'Techrep': [1, 1, 1], 'AAseq': ['ELVISLIVES', 'ELVISLIVES', 'ELVISLIVES'], 'mz':[500.0, 500.5, 501.0]}
df_a = pd.DataFrame(dict_data)
dict_data = {'Treatment': ['C', 'C', 'C'], 'Biorep': ['A', 'A', 'A'], 'Techrep': [1, 1, 1], 'AAseq': ['ELVISLIVES', 'ELVISLIVES', 'ELVISLIVES'], 'inte':[1100.0, 1050.0, 1010.0]}
df_b = pd.DataFrame(dict_data)

DF_A

        AAseq   Biorep  Techrep Treatment   mz
0    ELVISLIVES  A   1   C   500.0
1    ELVISLIVES  A   1   C   500.5
2    ELVISLIVES  A   1   C   501.0

DF_B

    AAseq   Biorep  Techrep Treatment   int
0    ELVISLIVES  A   1   C   1100
1    ELVISLIVES  A   1   C   1050
2    ELVISLIVES  A   1   C   1010

我可以通過以下方式添加列:

df_m = df_a.copy()
df_m['inte'] = df_b['inte']

     AAseq  Biorep  Techrep Treatment   inte
0    ELVISLIVES  A   1   C   1100
1    ELVISLIVES  A   1   C   1050
2    ELVISLIVES  A   1   C   1010

我的真實數據看起來要復雜得多,而且我擔心上述方法可能導致行中值的順序錯誤(特別是因為我想事先使用“ pandas.melt”)。

使用時:

dfm = pd.concat([df_a, df_b])

     AAseq  Biorep  Techrep Treatment   inte    mz
0    ELVISLIVES  A   1   C   NaN     500.0
1    ELVISLIVES  A   1   C   NaN     500.5
2    ELVISLIVES  A   1   C   NaN     501.0
0    ELVISLIVES  A   1   C   1100    NaN
1    ELVISLIVES  A   1   C   1050    NaN
2    ELVISLIVES  A   1   C   1010    NaN

串聯的DataFrame將值逐行擴展到NaN val。

問題 :如何使用“ concat”獲得相同的結果(如上所示)?

謝謝您的支持!

運用

 print pd.concat((df_a, df_b['inte']), axis=1)

你可以得到

        AAseq Biorep  Techrep Treatment     mz  inte
0  ELVISLIVES      A        1         C  500.0  1100
1  ELVISLIVES      A        1         C  500.5  1050
2  ELVISLIVES      A        1         C  501.0  1010

這是您所期望的嗎?


或者,也許您有像這樣更復雜的數據-在“ Treatment列中查看不同的值

        AAseq Biorep  Techrep Treatment     mz
0  ELVISLIVES      A        1         A  500.0
1  ELVISLIVES      A        1         B  500.5
2  ELVISLIVES      A        1         C  501.0

        AAseq Biorep  Techrep Treatment  inte
0  ELVISLIVES      A        1         C  1100
1  ELVISLIVES      A        1         B  1050
2  ELVISLIVES      A        1         A  1010

並且您需要使用AAseq Biorep Techrep Treatment列中的值來保持順序,然后使用merge

import pandas as pd
dict_data = {
    'AAseq': ['ELVISLIVES', 'ELVISLIVES', 'ELVISLIVES'],
    'Biorep': ['A', 'A', 'A'],
    'Techrep': [1, 1, 1],
    'Treatment': ['A', 'B', 'C'],
    'mz':[500.0, 500.5, 501.0]
}
df_a = pd.DataFrame(dict_data)

dict_data = {
    'AAseq': ['ELVISLIVES', 'ELVISLIVES', 'ELVISLIVES'],
    'Biorep': ['A', 'A', 'A'],
    'Techrep': [1, 1, 1],
    'Treatment': ['C', 'B', 'A'],
    'inte':[1100.0, 1050.0, 1010.0]
}
df_b = pd.DataFrame(dict_data)

print pd.merge(left=df_a, right=df_b, on=['AAseq', 'Biorep', 'Techrep', 'Treatment'])

結果:

        AAseq Biorep  Techrep Treatment     mz  inte
0  ELVISLIVES      A        1         A  500.0  1010
1  ELVISLIVES      A        1         B  500.5  1050
2  ELVISLIVES      A        1         C  501.0  1100

暫無
暫無

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

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