두 개의 pandas dataframe을 추가하는 방법(예제 포함)
다음 기본 구문을 사용하여 두 개의 Pandas DataFrame을 단일 DataFrame에 추가할 수 있습니다.
big_df = pd. concat ([df1, df2], ignore_index= True )
다음 예에서는 이 구문을 실제로 사용하는 방법을 보여줍니다.
예 1: 두 개의 Pandas DataFrame 추가
다음 코드는 두 개의 Pandas DataFrame을 단일 DataFrame에 추가하는 방법을 보여줍니다.
import pandas as pd #create two DataFrames df1 = pd. DataFrame ({' x ': [25, 14, 16, 27, 20, 12, 15, 14, 19], ' y ': [5, 7, 7, 5, 7, 6, 9, 9, 5], ' z ': [8, 8, 10, 6, 6, 9, 6, 9, 7]}) df2 = pd. DataFrame ({' x ': [58, 60, 65], ' y ': [14, 22, 23], ' z ': [9, 12, 19]}) #append two DataFrames together combined = pd. concat ([df1, df2], ignore_index= True ) #view final DataFrame combined X Y Z 0 25 5 8 1 14 7 8 2 16 7 10 3 27 5 6 4 20 7 6 5 12 6 9 6 15 9 6 7 14 9 9 8 19 5 7 9 58 14 9 10 60 22 12 11 65 23 19
예시 2: 2개 이상의 Pandas DataFrame 추가
pd.concat() 함수를 사용하여 두 개 이상의 팬더 DataFrame을 함께 추가할 수 있습니다.
import pandas as pd #create three DataFrames df1 = pd. DataFrame ({' x ': [25, 14, 16], ' y ': [5, 7, 7]}) df2 = pd. DataFrame ({' x ': [58, 60, 65], ' y ': [14, 22, 23]}) df3 = pd. DataFrame ({' x ': [58, 61, 77], ' y ': [10, 12, 19]}) #append all three DataFrames together combined = pd. concat ([df1, df2, df3], ignore_index= True ) #view final DataFrame combined x y 0 25 5 1 14 7 2 16 7 3 58 14 4 60 22 5 65 23 6 58 10 7 61 12 8 77 19
ignore_index 인수를 사용하지 않으면 결과 DataFrame의 인덱스는 각 개별 DataFrame에 대한 원래 인덱스 값을 유지합니다.
#append all three DataFrames together combined = pd. concat ([df1, df2, df3]) #view final DataFrame combined x y 0 25 5 1 14 7 2 16 7 0 58 14 1 60 22 2 65 23 0 58 10 1 61 12 2 77 19
여기에서 pandas.concat() 함수에 대한 전체 온라인 설명서를 찾을 수 있습니다.
추가 리소스
다음 튜토리얼에서는 Pandas에서 다른 일반적인 기능을 수행하는 방법을 설명합니다.
Pandas fillna()를 사용하여 NaN 값을 바꾸는 방법
여러 열에 걸쳐 Pandas DataFrame을 병합하는 방법
인덱스에 두 개의 Pandas DataFrame을 병합하는 방법