본문 바로가기
DataScience/Python

Pandas 행과 열의 문자열인덱스를 숫자로 가져오는 방법

by leopard4 2022. 12. 16.
# pandas 문자열로 된 인덱스를 숫자로 가져오는방법
# Create a sample dataframe
df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6], 'C': [7, 8, 9]}, index=['a', 'b', 'c'])

# Find the index number of label 'b'
index_number = df.index.get_loc('b')

print(index_number)  # Output: 1

# pandas 문자열로 된 컬럼을 숫자로 가져오는방법
# Create a sample dataframe
df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6], 'C': [7, 8, 9]})

# Find the index number of column 'B'
column_index = df.columns.get_loc('B')

print(column_index)  # Output: 1