반응형

“10 minutes to pandas” 문서를 따라해보며 작성했습니다.

https://pandas.pydata.org/pandas-docs/stable/user_guide/10min.html




2022. 02. 02  최초작성

2022. 03. 20

 

연결하기

concat 메소드를 사용하여 열방향으로 DataFrame을 연결합니다. 

 

import numpy as np
import pandas as pd


df1 = pd.DataFrame(np.arange(8).reshape(2,4))
print(df1.shape)
print(df1)
print()

df2 = pd.DataFrame(np.arange(8,16).reshape(2,4))
print(df2.shape)
print(df2)
print()


df3 = pd.DataFrame(np.arange(16,24).reshape(2,4))
print(df3.shape)
print(df3)
print()


df = pd.concat([df1, df2, df3])
print(df.shape)
print(df)
print()



concat 메소드를 사용하여 행방향으로 DataFrame을 연결합니다. 

 

import numpy as np
import pandas as pd


df1 = pd.DataFrame(np.arange(8).reshape(2,4))
print(df1.shape)
print(df1)
print()


df2 = pd.DataFrame(np.arange(8,16).reshape(2,4))
print(df2.shape)
print(df2)
print()


df = pd.concat([df1, df2], axis=1)
print(df.shape)
print(df)

 



append 메소드 사용하여 DataFrame을 연결하는 방법입니다. 열 방향으로 연결됩니다.

 

import numpy as np
import pandas as pd


df1 = pd.DataFrame(np.arange(8).reshape(2,4))
print(df1.shape)
print(df1)
print()

df2 = pd.DataFrame(np.arange(8,16).reshape(2,4))
print(df2.shape)
print(df2)
print()

result = df1.append(df2)
print(result.shape)
print(result)

 

경고문으로 append 대신에 concat을 사용하라는 메시지가 다음처럼 출력됩니다. 

 

/var/folders/hw/kk3pjps15015_hk0lq9wfjym0000gn/T/ipykernel_23042/4132674957.py:15: FutureWarning: The frame.append method is deprecated and will be removed from pandas in a future version. Use pandas.concat instead.

  result = df1.append(df2)



그룹핑(Grouping) 하기

 

지정한 열의 값을 기준으로 그룹핑합니다.

 

import numpy as np
import pandas as pd


df = pd.DataFrame(
{
    "A": ["foo", "bar", "foo", "bar", "foo", "bar", "foo", "foo"],
    "B": ["one", "one", "two", "three", "two", "two", "one", "three"],
    "C": [   1, 2, 3, 4,   5, 6, 7, 8],
    "D": [  -1,   -2, -3, -4,  -5,   -6, -7, -8],
}
)


print(df)
print()


# A열을 기준으로 그룹핑한 결과를 살펴봅니다.
print(df.groupby("A").apply(print))
print()


# A열을 기준으로 그룹핑한 결과에 sum() 메소드를 적용합니다.
print(df.groupby("A").sum())
print()


# B열을 기준으로 그룹핑한 결과를 살펴봅니다.
print(df.groupby("B").apply(print))
print()


# B열을 기준으로 그룹핑한 결과에 sum() 메소드를 적용합니다.
print(df.groupby("B").sum())

 



두 개의 열을 기준으로 그룹핑합니다.

 

import numpy as np
import pandas as pd


df = pd.DataFrame(
{
    "A": ["foo", "bar", "foo", "bar", "foo", "bar", "foo", "foo"],
    "B": ["one", "one", "two", "three", "two", "two", "one", "three"],
    "C": [   1, 2, 3, 4,   5, 6, 7, 8],
    "D": [  -1,   -2, -3, -4,  -5,   -6, -7, -8],
}
)


print(df)
print()


# A열, B열 기준으로 그룹핑한 결과를 살펴봅니다.
print(df.groupby(["A", "B"]).apply(print))
print()


# A열, B열 기준으로 그룹핑한 결과에 sum() 메소드를 적용합니다.
print(df.groupby(["A", "B"]).sum())

--




Pandas 강좌 1 - Pandas 객체 생성

https://webnautes.tistory.com/1957

 

Pandas 강좌 2 - 데이터 보는 방법

https://webnautes.tistory.com/1958

 

Pandas 강좌 3 - 데이터 선택하는 방법

https://webnautes.tistory.com/1959

 

Pandas 강좌 4 - 연산(Operations)

https://webnautes.tistory.com/1960

 

Pandas 강좌 5 - 연결 및 그룹핑

https://webnautes.tistory.com/1961

 

Pandas 강좌 6 - 시계열(Time series)

https://webnautes.tistory.com/1962

 

Pandas 강좌 7 - 그래프 그리기(Plotting)

https://webnautes.tistory.com/1963

 

Pandas 강좌 8 - Pandas에서 CSV, HDF5, Excel로 저장 및 읽기

https://webnautes.tistory.com/1964

 

Pandas 강좌 9 - 결측치(Missing data)

https://webnautes.tistory.com/1965




반응형

문제 발생시 지나치지 마시고 댓글 남겨주시면 가능한 빨리 답장드립니다.

도움이 되셨다면 토스아이디로 후원해주세요.
https://toss.me/momo2024


제가 쓴 책도 한번 검토해보세요 ^^

+ Recent posts