여러 열에서 고유한 값을 찾으려면 unique() 메서드를 사용하십시오. Pandas DataFrame에 "EmpName" 및 "Zone"이 있는 직원 레코드가 있다고 가정해 보겠습니다. 두 명의 직원이 비슷한 이름을 가질 수 있고 구역에 두 명 이상의 직원이 있을 수 있으므로 이름과 구역이 반복될 수 있습니다. 이 경우 고유한 Employee 이름을 원하면 DataFrame에 unique()를 사용하십시오.
먼저 필요한 라이브러리를 가져옵니다. 여기에서 pd를 별칭으로 설정했습니다 -
import pandas as pd
먼저 DataFrame을 만듭니다. 여기에 두 개의 열이 있습니다 -
dataFrame = pd.DataFrame( { "EmpName": ['John', 'Ted', 'Jacob', 'Scarlett', 'Ami', 'Ted', 'Scarlett'],"Zone": ['North', 'South', 'South', 'East', 'West', 'East', 'North'] } )
DataFrame 열 "EmpName" 및 "Zone"에서 고유한 직원 이름 및 영역 가져오기 -
{pd.concat([dataFrame['EmpName'],dataFrame['Zone']]).unique()}
예시
다음은 전체 코드입니다 -
import pandas as pd # Create DataFrame dataFrame = pd.DataFrame( { "EmpName": ['John', 'Ted', 'Jacob', 'Scarlett', 'Ami', 'Ted', 'Scarlett'],"Zone": ['North', 'South', 'South', 'East', 'West', 'East', 'North'] } ) print("DataFrame ...\n",dataFrame) # Fetch unique values from multiple columns print(f"\nFetching unique Values from the two columns and concatenate them:\n \ {pd.concat([dataFrame['EmpName'],dataFrame['Zone']]).unique()}")
출력
이것은 다음과 같은 출력을 생성합니다 -
DataFrame ... EmpName Zone 0 John North 1 Ted South 2 Jacob South 3 Scarlett East 4 Ami West 5 Ted East 6 Scarlett North Fetching unique Values from the two columns and concatenate them: ['John' 'Ted' 'Jacob' 'Scarlett' 'Ami' 'North' 'South' 'East' 'West']