Python의 pandas.concat() 함수
pandas.concat() 함수는 축과 함께 연결 작업을 수행하는 모든 무거운 작업을 수행합니다. 팬더 개체 다른 축에 있는 인덱스(있는 경우)의 선택적 설정 논리(합집합 또는 교차점)를 수행하는 동안.
Pandas concat() 함수 구문
통사론: concat(objs, 축, 조인,ignore_index, 키, 레벨, 이름, verify_integrity, 정렬, 복사)
매개변수:
- obs: 시리즈 또는 DataFrame 객체
- 중심선: 연결할 축; 기본값 = 0
- 가입하다: 방법 다른 축의 인덱스를 처리합니다. 기본값 = '외부'
- 무시_색인: True인 경우 연결 축을 따라 인덱스 값을 사용하지 않습니다. 기본값 = 거짓
- 키: 결과 인덱스에 식별자를 추가하는 시퀀스; 기본값 = 없음
- 레벨: MultiIndex를 구성하는 데 사용할 특정 수준(고유 값) 기본값 = 없음
- 이름: 결과 계층 인덱스의 수준 이름 기본값 = 없음
- verify_무결성: 새로 연결된 축에 중복이 포함되어 있는지 확인하세요. 기본값 = 거짓
- 종류: 조인이 '외부'일 때 아직 정렬되지 않은 경우 비연결 축을 정렬합니다. 기본값 = 거짓
- 복사: False인 경우 불필요하게 데이터를 복사하지 않습니다. 기본값 = True
보고: objs 유형(DataFrame 시리즈)
예제와 함께 Pandas를 사용하여 연결
예시 1: Python에서 DataFrame 연결
이 예에서는 기본 매개변수를 사용하여 두 계열을 연결합니다. 팬더 .
파이썬3
# importing the module> import> pandas as pd> # creating the Series> series1> => pd.Series([> 1> ,> 2> ,> 3> ])> display(> 'series1:'> , series1)> series2> => pd.Series([> 'A'> ,> 'B'> ,> 'C'> ])> display(> 'series2:'> , series2)> # concatenating> display(> 'After concatenating:'> )> display(pd.concat([series1, series2]))> |
산출
예시 2: 인덱스 = 1로 두 개의 데이터 프레임을 수평으로 결합하는 Pandas
이 예에서는 두 개의 Pandas 시리즈( series1> 그리고 series2> )를 사용하여 열(축=1)을 따라 연결합니다. pd.concat()> . 결과 DataFrame에는 두 Series가 모두 열로 포함되어 두 개의 열이 있는 새 DataFrame이 생성됩니다.
파이썬3
# importing the module> import> pandas as pd> # creating the Series> series1> => pd.Series([> 1> ,> 2> ,> 3> ])> display(> 'series1:'> , series1)> series2> => pd.Series([> 'A'> ,> 'B'> ,> 'C'> ])> display(> 'series2:'> , series2)> # concatenating> display(> 'After concatenating:'> )> display(pd.concat([series1, series2],> > axis> => 1> ))> |
산출
예시 3: 2개의 DataFrame 연결 및 키 할당
두 개의 DataFrame을 생성합니다( df1> 그리고 df2> )를 사용하여 각 DataFrame에 할당된 키와 함께 연결합니다. pd.concat()> . 결과 DataFrame에는 각 데이터 세트의 출처를 구별하는 'key1' 및 'key2' 키가 있는 계층적 인덱스가 있습니다.
파이썬3
# importing the module> import> pandas as pd> # creating the DataFrames> df1> => pd.DataFrame({> 'A'> : [> 'A0'> ,> 'A1'> ,> 'A2'> ,> 'A3'> ],> > 'B'> : [> 'B0'> ,> 'B1'> ,> 'B2'> ,> 'B3'> ]})> display(> 'df1:'> , df1)> df2> => pd.DataFrame({> 'A'> : [> 'A4'> ,> 'A5'> ,> 'A6'> ,> 'A7'> ],> > 'B'> : [> 'B4'> ,> 'B5'> ,> 'B6'> ,> 'B7'> ]})> display(> 'df2:'> , df2)> # concatenating> display(> 'After concatenating:'> )> display(pd.concat([df1, df2],> > keys> => [> 'key1'> ,> 'key2'> ]))> |
산출
예시 4: 축 = 1을 사용하여 Pandas에서 DataFrame을 가로로 연결
두 개의 DataFrame을 생성합니다( df1> 그리고 df2> ), 다음을 사용하여 열(축=1)을 따라 연결합니다. pd.concat()> . 결과 DataFrame은 두 항목의 열을 결합합니다. df1> 그리고 df2> , 나란히 정렬 .
파이썬3
# importing the module> import> pandas as pd> # creating the DataFrames> df1> => pd.DataFrame({> 'A'> : [> 'A0'> ,> 'A1'> ,> 'A2'> ,> 'A3'> ],> > 'B'> : [> 'B0'> ,> 'B1'> ,> 'B2'> ,> 'B3'> ]})> display(> 'df1:'> , df1)> df2> => pd.DataFrame({> 'C'> : [> 'C0'> ,> 'C1'> ,> 'C2'> ,> 'C3'> ],> > 'D'> : [> 'D0'> ,> 'D1'> ,> 'D2'> ,> 'D3'> ]})> display(> 'df2:'> , df2)> # concatenating> display(> 'After concatenating:'> )> display(pd.concat([df1, df2],> > axis> => 1> ))> |
산출
예시 5: ignore_index = True로 2개의 DataFrame 연결
두 개의 DataFrame을 생성합니다( df1> 그리고 df2> )를 동일한 열로 만들고 다음을 사용하여 세로로 연결합니다. pd.concat()> ~와 함께 ignore_index=True> . 결과 DataFrame에는 원래 인덱스를 무시하고 연속 인덱스가 있습니다. df1> 그리고 df2> .
파이썬3
# importing the module> import> pandas as pd> # creating the DataFrames> df1> => pd.DataFrame({> 'A'> : [> 'A0'> ,> 'A1'> ,> 'A2'> ,> 'A3'> ],> > 'B'> : [> 'B0'> ,> 'B1'> ,> 'B2'> ,> 'B3'> ]})> display(> 'df1:'> , df1)> df2> => pd.DataFrame({> 'A'> : [> 'A4'> ,> 'A5'> ,> 'A6'> ,> 'A7'> ],> > 'B'> : [> 'B4'> ,> 'B5'> ,> 'B6'> ,> 'B7'> ]})> display(> 'df2:'> , df2)> # concatenating> display(> 'After concatenating:'> )> display(pd.concat([df1, df2],> > ignore_index> => True> ))> |
산출
예시 6: DataFrame을 시리즈와 연결
DataFrame을 생성합니다( df> ) 및 시리즈( series> )를 사용하여 열(축=1)을 따라 연결합니다. pd.concat()> . 결과 DataFrame은 다음의 열을 결합합니다. df> 그리고 시리즈를 나란히 정렬합니다. 참고: 표시 설명에 오타가 있습니다( df1> 대신에 df> ).
파이썬3
# importing the module> import> pandas as pd> # creating the DataFrame> df> => pd.DataFrame({> 'A'> : [> 'A0'> ,> 'A1'> ,> 'A2'> ,> 'A3'> ],> > 'B'> : [> 'B0'> ,> 'B1'> ,> 'B2'> ,> 'B3'> ]})> display(> 'df:'> , df1)> # creating the Series> series> => pd.Series([> 1> ,> 2> ,> 3> ,> 4> ])> display(> 'series:'> , series)> # concatenating> display(> 'After concatenating:'> )> display(pd.concat([df, series],> > axis> => 1> ))> |
산출