반응형

분류 전체보기 116

[Pandas] Data Merge

가격데이터, 수량데이터 가져오기 import pandas as pd %matplotlib inline price_df = pd.read_csv('./data/vegetable_price.csv') quantity_df = pd.read_csv('./data/vegetable_quantity.csv') price_df quantity_df Inner Join (Product 컬럼 기준으로 데이터 합치기) pd.merge(price_df, quantity_df, on='Product') - Product 컬럼에서 공통적으로 가지고 있는 값만 출력 Left Outer Join pd.merge(price_df, quantity_df, on='Product', how='left') - price_df 에 있는 c..

Python/Pandas 2024.01.22

[Pandas] groupby 카테고라이징

import pandas as pd df = pd.read_csv('data/occupations.csv') occ_groups = df.groupby('occupation') # type(age_groupsBy) mean_occ_groups = occ_groups.mean(numeric_only=True) mean_occ_groups.sort_values('age') 직업별 여성 비율 높은 순서 구하기 import pandas as pd df = pd.read_csv('data/occupations.csv') # 여기에 코드를 작성하세요 # df.head() occ_group = df.groupby('occupation') df_male = df.loc[df['gender']=='M'] male_gro..

Python/Pandas 2024.01.22

[Pandas] 문자열 필터링

데이터 컬럼 출력 df['Genre'].unique() 문자열 포함 데이터 출력 df[df['Genre'].str.contains('Blues')] 특정 문자열로 시작하는 데이터 출력 df[df['Genre'].str.startswith('Blues')] 새로운 열에 값 넣기 df['Contains Blue'] = df[df['Genre'].str.contains('Blues')] 문자열 쪼개기 address = df['소재도로명주소'].str.split(n=1, expand=True) df['관할구역'] = address[0] import pandas as pd df = pd.read_csv('data/museum_2.csv') phone_number = df['운영기관전화번호'].str.split(p..

Python/Pandas 2024.01.16

[Seaborn] EDA

EDA(Exploratory Data Analysis) : 주어진 데이터를 다양한 관점에서 탐색하며 인사이트를 도출 - row의 의미 - column의 분포 - 여러 column간 연관성 기본정보 데이터 불러오기 import pandas as pd import seaborn as sns %matplotlib inline df = pd.read_csv('./data/young_survey.csv') df.head() basic_info = df.iloc[:, 140:] # 마지막 7개 column basic_info.head() - 성별 / 우세 손 / 학벌 column 요소별 갯수 basic_info['Gender'].value_counts() basic_info['Handedness'].value_co..

Python/Seaborn 2024.01.09

[Seaborn] LM Plot, Cat Plot

LM (Linear Model) sns.lmplot(data=body_df, x='Height', y='Weight' ) ※ 만약 그래프가 안 뜬다면?? 해결 1) !pip install --upgrade --ignore-installed pip setuptools : 패키지 버전 수정 해결 2) 패키지 버전 수정 !pip install clyent==1.2.2 !pip install PyYAML==6.0.1 해결 3) pip 버전 수정 !pip install -U pip Cat Plot 데이터 가져오기 import pandas as pd import seaborn as sns laptops_df = pd.read_csv('./data/laptops.csv') laptops_df.head() - OS 종..

Python/Seaborn 2024.01.08

[Seaborn] KDE

Seaborn : Statistical Data Visualization 확률밀도함수 PDF (Probability Desity Function) : 데이터 셋의 분포를 나타냄 : 특정 구간에서의 확률을 나타냄 선의 확률은 0 키가 170일 확률 = 0 KDE(Kernel Density Estimation) : 우리가 갖고 있는 데이터는 굴곡이 많은 데이터임 : KDE를 사용해 굴곡 많은 데이터를 추측을 통해 진짜 분포에 가깝게 부드러운 곡선으로 바꿀 수 있음 키 몸무게 데이터 가져오기 body_df = pd.read_csv('./data/body.csv', index_col=0) body_df.head() 데이터 정렬 body_df['Height'].value_counts().sort_index() 그..

Python/Seaborn 2024.01.05

[Pandas] 데이터 클리닝

갖추어야 할 조건 : 완결성 / 유일성 / 통일성 / 정확성 완결성 (Completeness) : 필수정보가 누락됨이 없이 전부 존재해야 함 NaN 값이 있는 데이터 df = pd.read_csv('./data/attendance.csv', index_col=0) print(df, end='\n\n') 결측값 합계 isnull() print(df.isnull().sum(), end='\n\n') dropna(행) df.dropna(inplace=True) print(df, end='\n\n') dropna(column) df.dropna(axis='columns', inplace=True) print(df, end='\n\n') NaN을 0으로 대체 df.fillna(0, inplace=True) pri..

Python/Pandas 2024.01.05

[Pandas] DataFrame index/column

rename 함수 - position 컬럼을 Position으로 대문자 변경 - columns 사용 liverpool_df = pd.read_csv('./data/liverpool.csv', index_col=0) liverpool_df.rename(columns={'position' : 'Position'}) print(liverpool_df, end='\n\n') ∴ 바뀌지 않음 - rename 함수가 새로운 DataFrame을 만들어 컬럼을 변경 → 원래 DataFrame에는 영향이 없음 → inplace=True 적용 liverpool_df = pd.read_csv('./data/liverpool.csv', index_col=0) liverpool_df.rename(columns={'positio..

Python/Pandas 2024.01.02

[Pandas] DataFrame 값 쓰기 및 삭제

데이터 row 값 추가 iphone_df.loc['iPhone XR'] = ['2018-01-25', 6.1, '3GB', 'ios 12.0.1', 'Yes'] print(iphone_df, end='\n\n') 데이터 column 추가 iphone_df['제조사'] = 'Apple' print(iphone_df, end='\n\n') 데이터 row 삭제 iphone_df.drop('iPhone XS', axis='index', inplace=False) # row 삭제 print(iphone_df, end='\n\n') - inplace=True인 경우 - inplace=False인 경우 -- 원본 데이터 안바뀜 데이터 column 삭제 iphone_df.drop('제조사', axis='columns'..

Python/Pandas 2024.01.01
반응형