Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |
Tags
- 영문자 확인
- 점프와 순간이동
- 최소공배수
- Java
- 반복문
- 순열
- 규칙찾기
- 조합
- HashSet
- python
- Stack
- 보이어무어
- 튜플
- fragment identifier
- 후위 표기법
- 에라토스테네스의 체
- 어려웠던 문제
- HashMap
- 메뉴리뉴얼
- 쿼드압축 후 개수세기
- 프로그래머스
- 동적계획법
- 알고리즘
- 완전 탐색
- dfs
- Dynamic Programming
- 2017 카카오 코드
- pandas
- 문자열
- 완전탐색
Archives
- Today
- Total
csmoon1010의 SW 블로그
[200712] Pandas 기초 공부 _ 행, 열 다루기 본문
인프런의 Pandas 데이터 분석 기초 강의를 참고하여 정리하였다.
1. 데이터프레임 행, 열 선택 및 필터하기
1) 행 선택하기
(1) row index를 이용
: df.loc[ ] 이용!
#연속적인 경우
df[1:3]
#불연속적인 경우
df.loc[[0, 2]]
(2) 컬럼의 condition에 따라 row 선택
#query 메소드 이용
df.query('age > 25')
#조건이 여러개인 경우(&, ㅣ)
df[ (df.age > 25) & (df.name == 'Nate') ]
2) 열 선택하기
(1) index 이용(df.iloc[ ])
#df.iloc[row, column]
df.iloc[:, 0:2]
(2) column name 이용
df.filter( ) 이용
df_filtered = df[['name', 'age']]
df.filter(items=['name', 'age'])
#like, regex 옵션 (column의 축 axis는 1로!!)
df.filter(like = 'a', axis = 1)
df.filter(regex = 'b$', axis = 1)
2. 데이터프레임 행, 열 삭제하기
df.drop( ) 이용하기
1) 행 삭제하기
#row id를 통해 삭제
df.drop(['John', 'Nate'])
df.drop(df.index[[0, 2]])
#삭제를 바로 반영(inplace)
df.drop(['John', 'Nate'], inplace = True)
#조건에 따라 삭제 = 조건에 따라 filtering
df[df.age > 20]
2) 열 삭제하기
#열 이름으로 삭제하기(axis = 1)
df.drop('age', axis = 1)
#삭제 바로 반영하기(inplace)
df.drop('age', axis = 1, inplace = True)
3. 데이터 프레임 행, 열 생성 및 수정하기
1) 열 추가
(1) 기본 : 리스트 인덱스 요소 쓰듯이
# 열 추가(모두 같은 값으로)
df['salary'] = 0
(2) Numpy를 이용해 조건에 따라 추가
import numpy as np
df['salary'] = np.where(df['job'] != 'student', 'yes', 'no')
df.head()
(3) 함수 형성 후 apply를 통해 적용
# pass or fail
def pass_or_fail(row):
if row != 'F':
return 'Pass'
else:
return 'Fail'
df.grade = df.grade.apply(pass_or_fail)
# 연도가 뽑아내기
def extract_year(row):
return row.split('-')[0]
df['year'] = df['yyyy-mm-dd'].apply(extract_year)
2) 행 추가
: 두 개의 dataframe의 구경꾼 역할.
- final 연도 : ignore_index = True
df2 = pd.DataFrame([
['Nate', 50, 50]
], columns = ['name', 'midterm', 'final'])
df.append(df2, ignore_index = True)
'데이터&인공지능 > 데이터분석(Python)' 카테고리의 다른 글
[200712] Pandas 기초 공부 _ 개념, 데이터 프레임 만들기 (직접, 파일), 파일로 저장하기 (0) | 2020.07.12 |
---|---|
[200711] Python 간단정리 _ 객체와 클래스 (0) | 2020.07.12 |
[200711] Python 간단정리 _ 함수, 모듈 (0) | 2020.07.11 |
[200711] Python 간단정리 _ 자료구조 (0) | 2020.07.11 |
[200711] Python 간단정리 _ 제어문 (0) | 2020.07.11 |
Comments