Notice
Recent Posts
Recent Comments
Link
초보 개발자의 일기
Pandas - Operations 본문
연산
이진(Binary) 연산
Stats (통계 데이터)
mean
df1.mean(0)
df1.mean()과 같다. (0은 열 기준, 1은 index 기준) : 평균값
A 0.135823
B 1.394061
C -0.523802
D -0.500769
F 2.750000
E 1.750000
E 1.750000
dtype: float64
shift
# 예시용 Series 생성
s = pd.Series([1, 3, 5, np.nan, 6, 8]. index=dates)
s
2013-01-01 1.0
2013-01-02 3.0
2013-01-03 5.0
2013-01-04 NaN
2013-01-05 6.0
2013-01-06 8.0
Freq: D, dtype: float64
shift로 값을 이동한다.
s = pd.Series([1, 3, 5, np.nan, 6, 8], index=dates).shift(2)
s
2013-01-01 NaN
2013-01-02 NaN
2013-01-03 1.0
2013-01-04 3.0
2013-01-05 5.0
2013-01-06 NaN
Freq: D, dtype: float64
sub
df의 각 값들에서 s를 뺀다. (원본 df ↗)
df.sub(s, axis='index')
shift에서 s는 0, 1, 5 행이 NaN이었는데, 원본 df에서 s를 빼면 아래처럼 0, 1, 5행이 NaN값이 된다.
Apply (함수를 적용)
df.apply(np.cumsum) # 누적합계를 구한다.
df.apply(lambda x: x.max() - x.min()) # 각 열의 최댓값, 최솟값 계산
A 2.223407
B 3.085018
C 2.360915
D 0.000000
F 4.000000
dtype: float64
Histogramming
s = pd.Series(np.random.randint(0, 7, size=10)) # 예시용 시리즈 생성
s
0 2
1 3
2 2
3 3
4 5
5 1
6 6
7 5
8 5
9 4
dtype: int32
각 숫자(Value)가 몇개인지 구하는 함수
s.value_counts()
5 3
3 2
2 2
6 1
4 1
1 1
dtype: int64
String Method
문자열 메소드
s = pd.Series(['A', 'B', 'C', 'Aaba', 'Baca', np.nan, 'CABA', 'dog', 'cat']) # 예시용
s
0 A
1 B
2 C
3 Aaba
4 Baca
5 NaN
6 CABA
7 dog
8 cat
dtype: object
Lower
s.str.lower()
문자를 소문자로 변환한다.
0 a
1 b
2 c
3 aaba
4 baca
5 NaN
6 caba
7 dog
8 cat
dtype: object
※ 10 minutes to pandas를 바탕으로 한 학습 기록입니다 ※
'소소한 공부 일기 > 데이터 분석' 카테고리의 다른 글
Pandas - Grouping (0) | 2021.05.09 |
---|---|
Pandas - Merge (0) | 2021.05.09 |
Pandas - Missing data (0) | 2021.05.08 |
Pandas - Selection (0) | 2021.05.08 |
Pandas - Viewing data (0) | 2021.05.08 |
Comments