빅데이터 분석기사,, 벼락치기용,, 기록용,, 을 남긴다.
실기 시험을 3일 앞두고 공부하는 나,,
다 까먹었기 때문에 문법이라도 기록해서 다음 실기 준비할 때 봐야겠다.
▼아래 '데이터 마님' 공부 방법과 문법을 참고해서 정리한다.
https://www.datamanim.com/dataset/99_pandas/pandasMain.html
1. csv 데이터 읽기
- tab 기준으로 열 나누기
df = pd.read_csv(DataUrl,sep='\t')
- 한글 인코딩
df = pd.read_csv(DataUrl, encoding='cp949')
2. 데이터 타입 확인
- 수치형 변수를 가진 컬럼만 출력(2가지)
print(df.columns[df.dtypes!='object'])
print(df.select_dtypes(exclude=object).columns)
- 범주형 변수를 가진 컬럼만 출력(2가지)
print(df.select_dtypes(include=object).columns)
print(df.columns[df.dtypes=='object'])
3. 데이터 확인
- 4분위 범위(IQR) 계산 : Q3-Q1
df['평균 속도'].quantile(0.75)-df['평균 속도'].quantile(0.25)
- 컬럼의 유일값 개수 출력(2가지)
print(len(df['읍면동명'].unique()))
print(df['읍면동명'].nunique())
- 컬럼 값이 3인 데이터를 추출하고 index를 0부터 재정렬
df.loc[df['quantity']==3].head().reset_index(drop=True)
- 조건 2개의 df 출력
df.loc[(df.new_price<=9) & (df.item_name=='Chicken Salad Bowl')].head()
df.loc[(df['item_name']=='Steak Salad') | (df['item_name']=='Bowl')]
- df의 짝수번째/ 홀수번째 컬럼만 출력
df.iloc[:, ::2]
df.iloc[:, 1::2]
- 컬럼 값에 따라 내림차순으로 정리
df.sort_values('new_price', ascending=False).reset_index(drop=True)
- 'Black'이 들어간 경우만 인덱싱
df.loc[df.choice_description.str.contains("Black")]
- "Vegetables"가 없는 index 개수
len(df.loc[~df.choice_description.str.contains('Vegetables')])
- 단어 수가 15개 이상인 index만 출력
df.loc[df.item_name.str.len()>=15]
- 리스트에 포함된 값이 있는 행만 출력
lst =[1.69, 2.39, 3.39, 4.45, 9.25, 10.98, 11.75, 16.98]
df.loc[df.new_price.isin(lst)]
4. 데이터 수정
- 컬럼 값의 달러 표시 문자를 제거하고 float형으로 변경해서 새로운 컬럼에 저장
df['new_price']=df['item_price'].str[1:].astype('float')
- 중복행 제거(첫번째 케이스/마지막 케이스만 남기기)
ans=ans.drop_duplicates('item_name')
ans = ans.drop_duplicates('item_name', keep='last')
- loc을 이용해 NaN인 데이터를 "NoData"로 수정
df.loc[df.choice_description.isnull(),'choice_description']='NoData'
5. Grouping
- 데이터의 'host_name' 의 빈도수를 구하고 이름으로 정렬한 상위 5개 출력
df['host_name'].value_counts().sort_index()
- 빈도수를 구하고 내림차순으로 정렬한 df 만들고 컬럼명 변경
df_1 = df.host_name.value_counts().to_frame()
df_1.rename(columns={'count':'counts'}, inplace=True)
- neighbourhood_group의 값에 따른 neighbourhood컬럼 값의 갯수
* as_index : True이면 group값으로 index를 새로 구성, False이면 기존 df의 인덱스를 따름
df.groupby(['neighbourhood_group','neighbourhood'], as_index=False).size()
- neighbourhood_group의 값에 따른 neighbourhood컬럼 값 중 neighbourhood_group그룹의 최댓값
df.groupby(['neighbourhood_group', 'neighbourhood'], as_index=False).size().groupby(['neighbourhood_group']).max()
- neighbourhood_group 값에 따른 price값의 평균, 분산, 최대, 최소 값을 구하기
df.groupby('neighbourhood_group')['price'].agg(['mean', 'var', 'max', 'min'])
- 계층적 index없이 구하고 nan값 처리
*stack : 컬럼을 인덱스로 , unstack : 인덱스를 컬럼으로
df.groupby(['neighbourhood', 'neighbourhood_group'])['price'].mean().unstack().fillna(-999)
'Python' 카테고리의 다른 글
[빅데이터 분석기사] 실기 7회 후기, 대구 시험장 주차장, 3일 준비(합격!!) (2) | 2023.12.04 |
---|---|
[빅데이터 분석기사] 실기 문제 연습 (작업형2) 모델링과 예측 (0) | 2023.12.01 |
[파이썬 라이브러리] Numpy, Pandas 문법 (0) | 2023.03.02 |
댓글