판다스 기본 예제
import pandas as pd
import numpy as np
# 파이썬은 경로가 \ 가아니라 / 로 표시한다 (파일 경로 주의)
'''
#기본 data 불러오기.
df = pd.read_excel("./input/test.xlsx", engine = "openpyxl")
print(df)
'''
# 특정 sheet Dataframe 화 하기
'''
df = pd.read_excel("./input/test.xlsx", engine = "openpyxl", sheet_name="Sheet1")
print(df)
'''
# header 조절
"""
df = pd.read_excel("./input/test.xlsx", engine = "openpyxl", sheet_name="Sheet1" , header=4)
print(df)
"""
#특정 열 인덱스 지정 가능
# df = pd.read_excel("./input/test.xlsx", engine = "openpyxl", sheet_name="Sheet1" , header=4 , index_col="교양")
# print(df)
#특정 열의 인덱스만 dataframe 생성하고 싶을떄 usecols(알파벳말고 숫자 인덱싱으로도 접근 가능하다. A,C열이면 usecols = [0, 2]를 입력해도 같은 결과이다.)
# df = pd.read_excel("./input/test.xlsx", engine = "openpyxl", sheet_name="Sheet1" , usecols="A,C")
# print(df)
# dataframe을 다시 엑셀 파일로 저장하기
#df.to_excel('test_save.xlsx')
df = pd.DataFrame([{"country":"한국","population":500},{"country":"미국","population":450},{"country":"싱가폴","population":705},
{"country":"호주","population":878},{"country":"베트남","population":660},{"country":"대만","population":808}])
# 특정 컬럼 = 의 값 추출 (ex 한국)
print(df[df['country'] == '한국'])
print(df[(df['country'] == '한국')|(df['country'] == '호주')])
print(df[(df['country']!='한국') & (df['population']>=800)])
# list에 없는 행 만 가져오기 (~ 없으면 각 행만 가져옴)
country_list = ['한국', '일본', '대만', '영국', '호주']
print(df[~df['country'].isin(country_list)])
참조 사이트:https://ybworld.tistory.com/42
[Python/파이썬] Pandas 기초 정리 : 엑셀 파일(.xlsx) Dataframe으로 만들기, Datarame을 다시 엑셀파일(xlsx)
※ 이 글을 쓰는 사람은 SW 비전공자입니다. ※ 개인 공부를 위해 정리하는 글이며, 작성한 코드들은 효율성, 깔끔함(?) 등과는 거리가 멀 수 있습니다. 1편 : 2021.03.31 - [코딩/Python] - [Python/파이썬]
ybworld.tistory.com
https://computer-science-student.tistory.com/375
[판다스, pandas] dataframe 특정 조건에 맞는 데이터 추출
pandas dataframe 특정 조건에 맞는 데이터 추출 pandas dataframe에서 특정 조건에 맞는 데이터를 추출하는 방법에 대해 정리하고자 한다. 데이터가 아래와 같이 있다고 가정한다. import pandas as pd df = pd.Da
computer-science-student.tistory.com
'공부,일 > 파이썬' 카테고리의 다른 글
txt 편집 하는 프로그램 (0) | 2023.02.05 |
---|---|
파이썬 배포하기 (0) | 2022.11.14 |
colab 환경설정 (0) | 2021.11.29 |
파이썬 엑셀 사용 (0) | 2021.11.23 |
원격으로 jupyter notebook 사용 colab (0) | 2021.11.17 |
댓글