prosource

판다에서 두 개의 데이터 프레임 행을 연결합니다.

probook 2023. 5. 13. 10:20
반응형

판다에서 두 개의 데이터 프레임 행을 연결합니다.

두 개의 데이터 프레임을 연결해야 합니다.df_a그리고.df_b행 수가 같은 경우(nRow) 키를 고려하지 않고 수평으로 설정합니다.이 기능은 다음과 유사합니다.cbindR 프로그래밍 언어로.각 데이터 프레임의 열 수는 다를 수 있습니다.

결과 데이터 프레임의 행 수는 동일합니다.nRow및 열 수는 두 데이터 프레임의 열 수를 합한 것과 같습니다.다시 말해, 이것은 두 개의 데이터 프레임의 맹목적인 기둥 연결입니다.

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 = {'Treatment1': ['C', 'C', 'C'], 'Biorep1': ['A', 'A', 'A'], 'Techrep1': [1, 1, 1], 'AAseq1': ['ELVISLIVES', 'ELVISLIVES', 'ELVISLIVES'], 'inte1':[1100.0, 1050.0, 1010.0]}
df_b = pd.DataFrame(dict_data)

호출 및 전달 매개 변수axis=1열 단위로 연결하는 방법:

In [5]:

pd.concat([df_a,df_b], axis=1)
Out[5]:
        AAseq Biorep  Techrep Treatment     mz      AAseq1 Biorep1  Techrep1  \
0  ELVISLIVES      A        1         C  500.0  ELVISLIVES       A         1   
1  ELVISLIVES      A        1         C  500.5  ELVISLIVES       A         1   
2  ELVISLIVES      A        1         C  501.0  ELVISLIVES       A         1   

  Treatment1  inte1  
0          C   1100  
1          C   1050  
2          C   1010  

온라인에서 병합, 결합연결하는 다양한 방법에 대한 유용한 가이드가 있습니다.

예를 들어, 충돌하는 열이 없으므로 행 수가 동일하므로 인덱스를 사용할 수 있습니다.

In [6]:

df_a.merge(df_b, left_index=True, right_index=True)
Out[6]:
        AAseq Biorep  Techrep Treatment     mz      AAseq1 Biorep1  Techrep1  \
0  ELVISLIVES      A        1         C  500.0  ELVISLIVES       A         1   
1  ELVISLIVES      A        1         C  500.5  ELVISLIVES       A         1   
2  ELVISLIVES      A        1         C  501.0  ELVISLIVES       A         1   

  Treatment1  inte1  
0          C   1100  
1          C   1050  
2          C   1010  

그리고 위와 같은 이유로 간단한 작업도 가능합니다.

In [7]:

df_a.join(df_b)
Out[7]:
        AAseq Biorep  Techrep Treatment     mz      AAseq1 Biorep1  Techrep1  \
0  ELVISLIVES      A        1         C  500.0  ELVISLIVES       A         1   
1  ELVISLIVES      A        1         C  500.5  ELVISLIVES       A         1   
2  ELVISLIVES      A        1         C  501.0  ELVISLIVES       A         1   

  Treatment1  inte1  
0          C   1100  
1          C   1050  
2          C   1010  

@EdChum 덕분에 저는 특히 인덱스가 일치하지 않을 때 같은 문제로 어려움을 겪고 있었습니다.안타깝게도 팬더 가이드에서는 이 사례가 누락되었습니다(예를 들어 일부 행을 삭제하는 경우).

import pandas as pd
t=pd.DataFrame()
t['a']=[1,2,3,4]
t=t.loc[t['a']>1] #now index starts from 1

u=pd.DataFrame()
u['b']=[1,2,3] #index starts from 0

#option 1
#keep index of t
u.index = t.index 

#option 2
#index of t starts from 0
t.reset_index(drop=True, inplace=True)

#now concat will keep number of rows 
r=pd.concat([t,u], axis=1)

인덱스 레이블이 다른 경우(예:df_a.index == [0, 1, 2]그리고.df_b.index == [10, 20, 30]이다True), 단도직입적으로join(또는)concat또는merge)는 NaN 행을 생성할 수 있습니다.이 경우 유용한 방법은 다음과 같습니다.set_axis()지수가 동일하도록 강제하는 것.

concatenated_df = df_a.join(df_b.set_axis(df_a.index))
# or 
concatenated_df = pd.concat([df_a, df_b.set_axis(df_a.index)], axis=1)

프레임의 길이가 동일한 경우, 다음과 같이 할당할 수도 있습니다.df_b로.df_a.와는 달리concat(또는)join또는merge), 변경 사항df_a새 데이터 프레임을 만들지 않습니다.

df_a[df_b.columns] = df_b

# if index labels are different
df_a[df_b.columns] = df_b.set_axis(df_a.index)

언급URL : https://stackoverflow.com/questions/28135436/concatenate-rows-of-two-dataframes-in-pandas

반응형