다음은 문자열 리스트를 데이터로 사용하고, index 매개변수를 사용해 인덱스를 지정하는 예제입니다.
# 인덱스를 지정해 시리즈 생성
fruits = ["apple", "banana", "cherry", "durian"]
index_labels = ["a", "b", "c", "d"]
series_str = pd.Series(fruits, index=index_labels)
# 시리즈 출력
print(series_str)
|
실행결과 |
a apple b banana c cherry d durian dtype: object |
index에 지정된 문자열 인덱스가 데이터에 적용된 것을 확인할 수 있습니다. 이제 0, 1, 2와 같은 정수형 인덱스뿐 아니라 "a", "b", "c" 같은 문자열 인덱스를 사용할 수 있습니다.