[엑셀과 함께 춤을] 열과 행을 제대로

앞서의 테스트 에서 코파일럿에게 무언가를 찾으라고 하면 컬럼으로 찾으려고 한다는 것을 알았습니다.

그러면 그렇게 엑셀을 바꾸어 주겠습니다. 기존 시트의 표를 90도 눕혀서 세로축과 가로축을 전치 시켰습니다.

image


표를 복사하여 붙여넣기 할 때 이 메뉴를 사용하면 됩니다.

image


아……제대로 됩니다. 제대로.

image

# 파일에서 두 번째 시트의 열 목록을 추출하는 작업
import pandas as pd

# 엑셀 파일 열기
excel_file = pd.ExcelFile("뭔가통합_전치_공백제거.xlsx")

# 두 번째 시트 이름 가져오기
sheet_name = excel_file.sheet_names[1]

# 해당 시트의 데이터프레임 생성
df = pd.read_excel(excel_file, sheet_name=sheet_name)

# 열 목록 출력
columns = df.columns.tolist()
print(columns)

원하는 거 됩니다.

image


코드도 깔끔해요. 이겁니다.

import pandas as pd

# Load the Excel file with the appropriate engine
excel_file = pd.ExcelFile("뭔가통합_전치_공백제거.xlsx", engine="openpyxl")

# Prepare a list to collect data from each sheet
combined_data = []

# Iterate through each sheet
for sheet_name in excel_file.sheet_names:
    # Read the sheet into a DataFrame
    df = pd.read_excel(excel_file, sheet_name=sheet_name)
    
    # Check if the target column exists
    if '영문 프롬프트 (구글번역기)' in df.columns:
        # Extract the column and add sheet name
        temp_df = pd.DataFrame({
            'Sheet Name': sheet_name,
            '영문 프롬프트 (구글번역기)': df['영문 프롬프트 (구글번역기)']
        })
        combined_data.append(temp_df)

# Concatenate all collected data into a single DataFrame
result_df = pd.concat(combined_data, ignore_index=True)

# Display the combined table
print(result_df)

시트도 열도 잘 인식해서 답변합니다.

image


아무렇게나 말해도 제대로 된 코드를 만들어서 수행합니다

import pandas as pd

# Load the Excel file with the appropriate engine
excel_file = pd.ExcelFile("뭔가통합_전치_공백제거.xlsx", engine="openpyxl")

# Get the name of the third sheet
third_sheet_name = excel_file.sheet_names[2]

# Read the third sheet into a DataFrame
df_third_sheet = pd.read_excel(excel_file, sheet_name=third_sheet_name, engine="openpyxl")

# Display the content of the third sheet
df_third_sheet