본문 바로가기
프로그래밍/파이썬(Python)

파이썬(Python) Pandas Series, Dataframe index #1

by comflex 2022. 12. 30.
728x90
반응형

Pandas Series는 색인(index)를 이용해서 각 요소에 접근할 수 있습니다. 색인은 문자열 값이나 정수 값을 사용할 수 있으며, 정수 값을 이용한 색인은 NumPy 배열과 유사합니다.

아래의 코드는 Pandas Series의 색인을 이용해서 각 요소에 접근하는 예입니다.

 

import pandas as pd

s = pd.Series([1, 3, 5, np.nan, 6, 8], index=['a', 'b', 'c', 'd', 'e', 'f'])

# 색인을 이용한 인덱싱
print(s['a'])  # 1.0
print(s[0])  # 1.0

# 슬라이싱을 이용한 인덱싱
print(s['a':'c'])  # a    1.0
                   # b    3.0
                   # c    5.0
                   # dtype: float64
print(s[0:3])  # a    1.0
               # b    3.0
               # c    5.0
               # dtype: float64

Pandas의 intersection() 메서드는 두 개의 Pandas Series 또는 Pandas DataFrame의 색인의 교집합을 반환하는 메서드입니다.  아래의 코드는 Pandas Series의 색인의 교집합을 구하는 예입니다.

 

import pandas as pd

# Pandas Series 생성
s1 = pd.Series([1, 2, 3, 4], index=['a', 'b', 'c', 'd'])
s2 = pd.Series([3, 4, 5, 6], index=['c', 'd', 'e', 'f'])

# intersection() 메서드 사용
intersection = s1.index.intersection(s2.index)
print(intersection)  # Index(['c', 'd'], dtype='object')

위의 코드에서는 intersection() 메서드를 사용하여 두 개의 Pandas Series의 색인의 교집합을 구하였습니다. intersection() 메서드의 결과는 Pandas Index 객체를 반환합니다.

 

andas의 union() 메서드는 두 개의 Pandas Series 또는 Pandas DataFrame의 색인의 합집합을 반환하는 메서드입니다. 예를 들어, 아래의 코드는 Pandas Series의 색인의 합집합을 구하는 예입니다.

import pandas as pd

# Pandas Series 생성
s1 = pd.Series([1, 2, 3, 4], index=['a', 'b', 'c', 'd'])
s2 = pd.Series([3, 4, 5, 6], index=['c', 'd', 'e', 'f'])

# union() 메서드 사용
union = s1.index.union(s2.index)
print(union)  # Index(['a', 'b', 'c', 'd', 'e', 'f'], dtype='object')

위의 코드에서는 union() 메서드를 사용하여 두 개의 Pandas Series의 색인의 합집합을 구하였습니다. union() 메서드의 결과는 Pandas Index 객체를 반환합니다

 

Pandas의 difference() 메서드는 두 개의 Pandas Series 또는 Pandas DataFrame의 색인의 차집합을 반환하는 메서드입니다. 예를 들어, 아래의 코드는 Pandas Series의 색인의 차집합을 구하는 예입니다.

import pandas as pd

# Pandas Series 생성
s1 = pd.Series([1, 2, 3, 4], index=['a', 'b', 'c', 'd'])
s2 = pd.Series([3, 4, 5, 6], index=['c', 'd', 'e', 'f'])

# difference() 메서드 사용
difference = s1.index.difference(s2.index)
print(difference)  # Index(['a', 'b'], dtype='object')

 

아래의 코드는 Pandas DataFrame의 색인의 교집합, 합집합, 차집합을 구하는 예입니다.

import pandas as pd

# Pandas DataFrame 생성
df1 = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6], 'C': [7, 8, 9]}, index=['a', 'b', 'c'])
df2 = pd.DataFrame({'A': [3, 4, 5], 'B': [6, 7, 8], 'C': [9, 10, 11]}, index=['c', 'd', 'e'])

# intersection() 메서드 사용
intersection = df1.index.intersection(df2.index)
print(intersection)  # Index(['c'], dtype='object')

# union() 메서드 사용
union = df1.index.union(df2.index)
print(union)  # Index(['a', 'b', 'c', 'd', 'e'], dtype='object')

# difference() 메서드 사용
difference = df1.index.difference(df2.index)
print(difference)  # Index(['a', 'b'], dtype='object')

2022.12.30 - [프로그래밍/파이썬(Python)] - 파이썬(Python) Pandas Series, Dataframe index #2

 

파이썬(Python) Pandas Series, Dataframe index #2

Pandas에서 인덱싱은 데이터프레임에서 특정 행과 열을 선택하는 것을 의미합니다. 인덱싱은 기본적으로 정수 인덱스를 사용하지만, 문자열 인덱스도 사용할 수 있습니다. Pandas에서 인덱싱을 할

com-flex.tistory.com

반응형

2022.08.03 - [프로그래밍/파이썬(Python)] - 파이썬(Python) Pandas Series

 

파이썬(Python) Pandas Series

Series Series는 파이썬 Pandas의 자료 구조 중 하나이다. 코드 import pandas as pd import numpy as np pd.__version__ #Pandas version 확인 s = pd.Series([0, 1, 2, 3, 4, 5]) print(s) 현재 사용중인 Pandas 의 버전은 version 명령으

com-flex.tistory.com

2022.12.30 - [프로그래밍/파이썬(Python)] - 파이썬(Python) Pandas Series 산술, 논리 연산

 

파이썬(Python) Pandas Series 산술, 논리 연산

Pandas Series는 다양한 연산을 지원합니다. 이들 연산은 요소별로 수행되며, 색인이 같은 요소끼리 연산이 수행됩니다. 이들 연산은 산술 연산, 비교 연산, 논리 연산, 집계 연산 등이 있습니다. 산

com-flex.tistory.com

 

728x90
반응형