Notice
Recent Posts
Recent Comments
Link
250x250
develope_kkyu
[Python] pandas 예제 - 4 본문
728x90
https://pandas.pydata.org/docs/user_guide/10min.html에 나와 있는 pandas 예제 코드를 살펴본다.
import pandas as pd
import numpy as np
Grouping(그룹화)
- 그룹화는 어떠한 기준을 바탕으로 데이터를 분할(split), 각 그룹에 어떤 함수를 독립적으로 적용(apply), 적용되어 나온 결과들을 결합(combine)하는 과정을 지칭한다.
df = pd.DataFrame(
{
"A": ["foo", "bar", "foo", "bar", "foo", "bar", "foo", "foo"],
"B": ["one", "one", "two", "three", "two", "two", "one", "three"],
"C": np.random.randn(8),
"D": np.random.randn(8),
}
)
df
Out[]:
A B C D
0 foo one 1.346061 -1.577585
1 bar one 1.511763 0.396823
2 foo two 1.627081 -0.105381
3 bar three -0.990582 -0.532532
4 foo two -0.441652 1.453749
5 bar two 1.211526 1.208843
6 foo one 0.268520 -0.080952
7 foo three 0.024580 -0.264610
df.groupby("A")[["C", "D"]].sum() # 'A' 칼럼 기준으로 'C','D' 칼럽 값 합계 구하기
Out[]:
C D
A
bar 1.732707 1.073134
foo 2.824590 -0.574779
df.groupby(["A", "B"]).sum() # 'A'와 'B' 칼럼 기준으로 값들의 합 구하기
Out[]:
C D
A B
bar one 1.511763 0.396823
three -0.990582 -0.532532
two 1.211526 1.208843
foo one 1.614581 -1.658537
three 0.024580 -0.264610
two 1.185429 1.348368
변형(Reshaping)
- Stack(압축)
tuples = list(
zip(
["bar", "bar", "baz", "baz", "foo", "foo", "qux", "qux"],
["one", "two", "one", "two", "one", "two", "one", "two"],
)
)
index = pd.MultiIndex.from_tuples(tuples, names=["first", "second"])
df = pd.DataFrame(np.random.randn(8, 2), index=index, columns=["A", "B"])
df2 = df[:4]
df2
Out[]:
A B
first second
bar one -0.727965 -0.589346
two 0.339969 -0.693205
baz one -0.339355 0.593616
two 0.884345 1.591431
stacked = df2.stack()
stacked # 칼럼을 인덱스화해서 압축하기
Out[]:
first second
bar one A -0.727965
B -0.589346
two A 0.339969
B -0.693205
baz one A -0.339355
B 0.593616
two A 0.884345
B 1.591431
dtype: float64
stack 메소드를 통해 압축된 데이터 프레임은 다시 unstack 메소드를 통해 원래대로 돌아올 수 있다.
stacked.unstack() # 압축된 데이터 프레임을 원래대로 돌려놓는다.
Out[]:
A B
first second
bar one -0.727965 -0.589346
two 0.339969 -0.693205
baz one -0.339355 0.593616
two 0.884345 1.591431
stacked.unstack(1) # 1을 입력하면 2번째 인덱스 기준으로 압축 해제
Out[]:
second one two
first
bar A -0.727965 0.339969
B -0.589346 -0.693205
baz A -0.339355 0.884345
B 0.593616 1.591431
stacked.unstack(0) # 0을 입력하면 1번째 인덱스 기준으로 압축 해제
Out[]:
first bar baz
second
one A -0.727965 -0.339355
B -0.589346 0.593616
two A 0.339969 0.884345
B -0.693205 1.591431
- Pivot tables
df = pd.DataFrame(
{
"A": ["one", "one", "two", "three"] * 3,
"B": ["A", "B", "C"] * 4,
"C": ["foo", "foo", "foo", "bar", "bar", "bar"] * 2,
"D": np.random.randn(12),
"E": np.random.randn(12),
}
)
df
Out[]:
A B C D E
0 one A foo -1.202872 0.047609
1 one B foo -1.814470 -0.136473
2 two C foo 1.018601 -0.561757
3 three A bar -0.595447 -1.623033
4 one B bar 1.395433 0.029399
5 one C bar -0.392670 -0.542108
6 two A foo 0.007207 0.282696
7 three B foo 1.928123 -0.087302
8 one C foo -0.055224 -1.575170
9 one A bar 2.395985 1.771208
10 two B bar 1.552825 0.816482
11 three C bar 0.166599 1.100230
pd.pivot_table(df, values="D", index=["A", "B"], columns=["C"]) # 피벗테이블 생성
Out[]:
C bar foo
A B
one A 2.395985 -1.202872
B 1.395433 -1.814470
C -0.392670 -0.055224
three A -0.595447 NaN
B NaN 1.928123
C 0.166599 NaN
two A NaN 0.007207
B 1.552825 NaN
C NaN 1.018601
728x90
'Python' 카테고리의 다른 글
[Python] MLB Stats API를 이용해 오타니, 저지 2022시즌 타격 성적 비교 (1) | 2023.01.19 |
---|---|
[Python] pandas 예제 - 5 (0) | 2023.01.17 |
[Python] pandas 예제 - 3 (0) | 2023.01.05 |
[Python] pandas 예제 - 2 (0) | 2023.01.04 |
[Python] pandas 예제 - 1 (1) | 2022.12.30 |