- 컬럼을 인덱스로 지정
df.set_index('컬럼명', inplace = True)
- 인덱스, 컬럼 추가
df['칼럼명'] = 값 아무거나 써도 다 같은 값으로 들어감
df.loc['인덱스명'] = 값 아무거나 써도 다 같은 값으로 들어감
- 일정 수준 이상이나 이하의 값만 보고 싶다
df[df 연산자 숫자]
#하면 데이터 프레임에 그 조건에 True 인지 False 인지 나옴
#총 몇 개인지 알고 싶다?
df[df 연산자 숫자].count().sum()
- 값 빈도수 확인
df['컬럼명'].value_counts()
- 삭제
#행 삭제
df.drop(인덱스 번호)
#열 삭제
df.drop(['컬럼명'], axis = 1)
- 기초통계량 = 평균, 최대/최소, 중앙값, 사분위 수
# 숫자형일때
df['컬럼명'].mean()
df['컬럼명'].median()
df['컬럼명'].describe() #전체
# 문자형일 때
df.describe(include='object') #->빈도수와 중복값을 count
- bar plot 만드는 거
import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
plt.style.use('ggplot')
import random
#pandas
#한 개의 그룹
df.plot.bar(self, x=None, y=None, **kwargs)
df_by_group.plot.bar(x='group',y='xval',rot=0) #세로
df_by_group.plot.barh(x='group',y='xval',rot=0) #가로
#두 개의 그룹
df_by_group_label = df_by_group_label.reset_index()
df_pivot = df_by_group_label.pivot(index='group',columns='label',values='xval')
df_pivot.plot.bar(rot=0) #두 개가 나란히
df_pivot.plot.bar(stacked=True, rot=0) #두 개가 세로로 합쳐져서
#mapplotlib
matplotlib.pyplot.bar(x, height, width=0.8, bottom=None, *, align=’center’, data=None, **kwargs)
#한 개의 그룹
#세로
label = df_by_group.index
index = np.arange(len(label)) # 0,1,2,3
plt.bar(index, df_by_group)
plt.xticks(index, label, fontsize=15) # label 이름 넣기
#가로
plt.barh(index, df_by_group)
plt.yticks(index, label, fontsize=15)
#두 개의 그룹
#두 개가 나란히
p1 = plt.bar(index,df_by_group_by0, color='red', alpha=0.5,
width=0.4)
p2 = plt.bar(index+0.4,df_by_group_by1, color='blue', alpha=0.5,
width=0.4)
plt.xticks(index,label)
plt.legend((p1[0], p2[0]), ('0', '1'), fontsize=15)
#두 개가 세로로 합쳐져서
p1 = plt.bar(index,df_by_group_by0, color='red', alpha=0.5)
p2 = plt.bar(index,df_by_group_by1, color='blue', alpha=0.5,
bottom=df_by_group_by0)
plt.xticks(index,label)
plt.legend((p1[0], p2[0]), ('0', '1'), fontsize=15)
#seaborn
seaborn.barplot(x=None, y=None, hue=None, data=None, order=None, hue_order=None, estimator=<function mean at 0x10a2a03b0>, ci=95, n_boot=1000, units=None, seed=None, orient=None, color=None, palette=None, saturation=0.75, errcolor=’.26’, errwidth=None, capsize=None, dodge=True, ax=None, **kwargs)
#한 개의 그룹
sns.barplot(x='group', y='xval', data=df_by_group)
#두 개의 그룹
sns.barplot(x='group', y='xval', hue='label',data=df_by_group_label )
도움되는 사이트
(참고)https://12jeong.github.io/python-barplot/
막대 그래프(Bar Chart) 그리는 방법-pandas, matplotlib, seaborn, ggplot2
요약 : 시각화할 때 막대 그래프 자주 사용하는데, 검색할 때 마다 방법이 너무 다양하다… 정리해보자.
12jeong.github.io
- matplot 사이트 : https://wikidocs.net/92071
01. Matplotlib 기본 사용
 Matplotlib 라이브러리를 이용해서 그래프를 그리는 일반적 ...
wikidocs.net
- groupby
#한 개 열을 기준으로 그룹객체 생성
df.groupby('컬럼명')
df.get_group('그룹이름')
#여러열을 기준으로 객체 생성
df.groupby(['컬럼명1', '컬럼명2']) #이러면 컬럼명1 value 가지수 * 컬럼명2 value 가지수 의 튜플수
#연산 메소드
mean(), max(), min(), sum(), count(), size(), var(), std(), describe(), info(), first(), last()
#여러개의 함수를 여러 열에 적용 : agg()
.agg(함수명)
#모든열에 여러 함수를 매핑 : group객체.agg([함수1,함수2,함수3,…])
#각 열마다 다른 함수를 매핑 : group객체.agg({‘열1’: 함수1, ‘열2’:함수2, …})
#filter 함수 : 개별 원소에 대한 필터링이 아니고 group객체를 필터링
.group객체.filter(조건식 함수)
(참고) https://yganalyst.github.io/data_handling/Pd_13/p
[Pandas 기초] 그룹(group)객체 생성 및 집계(agg) 연산
판다스의 groupby함수 및 다양한 그룹연산 메소드들에 대해 알아보자
yganalyst.github.io
Seaborn(SNS)를 사용한 파이썬 데이터 시각화 기초 matplotlib
데이터 과학을 공부하는 데 있어 필수적인 데이터 시각화에 대해서 공부하도록 하겠습니다. 오늘은 Seaborn 과 matplotlib를 사용하여 데이터를 시각화하는 방법에 대해서 알아보겠습니다. 데이터는
chancoding.tistory.com
QUIZ&CHALLENGE
- random.seed는 모든 사람이 random으로 돌려도 같은 숫자가 나오도록 하는 거. 안의 숫자는 추출되는 방식의 차이
np.random.seed(0)
'[코드스테이츠]AI' 카테고리의 다른 글
[S1-Week2]Confidence Interval (0) | 2022.04.06 |
---|---|
[S1-Week2]Hypothesis Test 2 (0) | 2022.04.06 |
[S1-Week1]Basic Derivative (0) | 2022.03.31 |
[S1-Week1]Data Manipulation (0) | 2022.03.30 |
[S1-Week1]Feature Engineering (0) | 2022.03.29 |
댓글