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
- 프로그래머스
- 자바
- 파이썬
- backjoon
- 스프링 부트 쇼핑몰 프로젝트 with JPA
- DB
- 데이터베이스
- postgresql
- 자료구조
- 네트워크
- springboot
- baekjoon
- Spring Security
- 데이터
- 플러터
- javascript
- spring
- python
- 리눅스
- CS지식
- 시큐리티
- 스프링부트
- Flutter
- Oracle
- CS
- 자바스크립트
- 백준
- 스프링
- JPA
- Java
Archives
- Today
- Total
Jin's Dev Story
[Python] DataFrame 본문
# DataFrame 실습
df = pd.DataFrame({'a':[2,3], 'b':[5, 10]})
df
df = pd.DataFrame([[2, 5], [3, 10], [10, 20]], columns=['a', 'b'])
df
데이터프레임 행, 열 삭제
df.drop([지울 행이나 열], axis=축(0 or 1))
열 지울 땐 axis = 1
행 지울 땐 axis = 0
df.drop(['B', 'C'], axis=1)
inplace = True
- 변수 할당 없이 변수에 적용하겠다는 뜻
df.drop(['B', 'C'], axis=1, inplace=True)
== 같은 뜻
df = df.drop(['B', 'C'], axis=1)
계산 기능
- mean() : 평균 값 구하기
데이터프레임 수정
# 1행의 C열의 값을 육으로 변경
df.loc[1, 'C'] = '육'
# 1행의 C의 값을 6으로 변경
df.iloc[1, 2] = 6
# 위 코드 두가지는 모두 동일한 뜻
조건 필터링 가져오기
- 불린 형태로 가져오기 때문에 df[ ]로 묶어주기
df[df['age']>=30]
# df['age']>=30의 경우엔 불린형태로 가져옴
# job이 intern인 사람 가져오기
df['job'] == 'intern'
df[df['job'] == 'intern']
- and, or 연산의 경우엔 ( )로 묶고 &, | 사용
# 조건 여러개
# 30대 이상 40대 이하
df[df['age'] >=30 & df['age'] <= 40]
>> 에러발생
df[(df['age'] >=30) & (df['age'] <= 40)]
# 조건 여러개
# 30대 미만 혹은 40대 초과
df[(df['age']<30) | (df['age']>40)]
- in을 사용하려면 왼쪽에 단일값이 와야함
- job에 있는 값은 lambda x에 넣을 건데 x가 ['student', 'manager']에 포함되니? 라고 묻는 것
df['job'].apply(lambda x : x in ['student', 'manager']) # in을 통한 포함 조건 걸기 df[df['job'].apply(lambda x : x in ['student', 'manager'])]
데이터프레임 복사 - df.copy()
- 혹시 모룰 실수를 대비하여 복사한 값으로 사용하기
'Programming Language > Python' 카테고리의 다른 글
[Python] apply 함수 (0) | 2024.06.13 |
---|---|
[Python] DataFrame 그룹 (0) | 2024.06.13 |
[Python] iloc와 loc (0) | 2024.06.13 |
[Python] Pandase(판다스) (0) | 2024.06.13 |
[Python] 시각화할 때 한글 깨지는 경우 (0) | 2024.06.13 |