<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
  <channel>
    <title>달나라 노트</title>
    <link>https://cosmosproject.tistory.com/</link>
    <description></description>
    <language>ko</language>
    <pubDate>Sun, 28 Jun 2026 17:27:29 +0900</pubDate>
    <generator>TISTORY</generator>
    <ttl>100</ttl>
    <managingEditor>CosmosProject</managingEditor>
    <image>
      <title>달나라 노트</title>
      <url>https://tistory1.daumcdn.net/tistory/4327363/attach/b996039b121c4a28a6b2eb07e65f5da3</url>
      <link>https://cosmosproject.tistory.com</link>
    </image>
    <item>
      <title>Python numpy : nan_to_num</title>
      <link>https://cosmosproject.tistory.com/943</link>
      <description>&lt;div style=&quot;font-family: 'Noto Sans KR', Apple SD Gothic Neo, sans-serif; font-size: 16px; line-height: 1.8; color: #333; max-width: 780px; margin: 0 auto;&quot;&gt;

  &lt;h1 style=&quot;font-size: 28px; font-weight: 700; color: #1a1a1a; border-bottom: 3px solid #4f8ef7; padding-bottom: 12px; margin-bottom: 32px;&quot;&gt;NumPy &lt;code style=&quot;background:#eef2ff; color:#4f46e5; padding:2px 8px; border-radius:4px; font-size:90%;&quot;&gt;nan_to_num()&lt;/code&gt; — NaN과 무한대를 안전한 값으로 교체하는 함수&lt;/h1&gt;

  &lt;p style=&quot;margin: 0 0 24px;&quot;&gt;&lt;code style=&quot;background:#eef2ff; color:#4f46e5; padding:2px 8px; border-radius:4px; font-size:90%;&quot;&gt;nan_to_num()&lt;/code&gt;은 배열에서 NaN, 양의 무한대(inf), 음의 무한대(-inf)를 지정한 값으로 교체하는 함수입니다. 수치 연산 전에 배열을 안전한 상태로 만들 때 씁니다.&lt;/p&gt;

  &lt;h2 style=&quot;font-size: 22px; font-weight: 700; color: #1a1a1a; margin-top: 40px; margin-bottom: 16px; position: sticky; top: 0; background: #eef4ff; border-left: 3px solid #4f8ef7; padding: 8px 14px; border-radius: 0 6px 6px 0; z-index: 10; box-shadow: 0 4px 16px rgba(0,0,0,0.08);&quot;&gt;기본 동작&lt;/h2&gt;

  &lt;pre style=&quot;background:#1e1e2e; color:#cdd6f4; padding:20px 24px; border-radius:8px; font-size:14px; line-height:1.7; overflow-x:auto; margin-bottom:24px;&quot;&gt;&lt;code&gt;np.nan_to_num(x)
&lt;span style=&quot;color:#6c7086;&quot;&gt;# NaN  → 0&lt;/span&gt;
&lt;span style=&quot;color:#6c7086;&quot;&gt;# inf  → 매우 큰 양수 (시스템 최댓값)&lt;/span&gt;
&lt;span style=&quot;color:#6c7086;&quot;&gt;# -inf → 매우 큰 음수 (시스템 최솟값)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

  &lt;h2 style=&quot;font-size: 22px; font-weight: 700; color: #1a1a1a; margin-top: 40px; margin-bottom: 16px; position: sticky; top: 0; background: #eef4ff; border-left: 3px solid #4f8ef7; padding: 8px 14px; border-radius: 0 6px 6px 0; z-index: 10; box-shadow: 0 4px 16px rgba(0,0,0,0.08);&quot;&gt;기본 문법&lt;/h2&gt;

  &lt;pre style=&quot;background:#1e1e2e; color:#cdd6f4; padding:20px 24px; border-radius:8px; font-size:14px; line-height:1.7; overflow-x:auto; margin-bottom:16px;&quot;&gt;&lt;code&gt;numpy.nan_to_num(x, copy=True, nan=0.0, posinf=None, neginf=None)&lt;/code&gt;&lt;/pre&gt;

  &lt;table style=&quot;width:100%; border-collapse:collapse; margin-bottom:24px; font-size:15px;&quot;&gt;
    &lt;thead&gt;
      &lt;tr style=&quot;background:#4f8ef7; color:#fff;&quot;&gt;
        &lt;th style=&quot;padding:10px 16px; text-align:left; border:1px solid #ddd;&quot;&gt;파라미터&lt;/th&gt;
        &lt;th style=&quot;padding:10px 16px; text-align:left; border:1px solid #ddd;&quot;&gt;기본값&lt;/th&gt;
        &lt;th style=&quot;padding:10px 16px; text-align:left; border:1px solid #ddd;&quot;&gt;설명&lt;/th&gt;
      &lt;/tr&gt;
    &lt;/thead&gt;
    &lt;tbody&gt;
      &lt;tr style=&quot;background:#f9fafb;&quot;&gt;
        &lt;td style=&quot;padding:10px 16px; border:1px solid #ddd;&quot;&gt;&lt;code style=&quot;background:#eef2ff; color:#4f46e5; padding:2px 6px; border-radius:4px;&quot;&gt;nan&lt;/code&gt;&lt;/td&gt;
        &lt;td style=&quot;padding:10px 16px; border:1px solid #ddd;&quot;&gt;&lt;code style=&quot;background:#eef2ff; color:#4f46e5; padding:2px 6px; border-radius:4px;&quot;&gt;0.0&lt;/code&gt;&lt;/td&gt;
        &lt;td style=&quot;padding:10px 16px; border:1px solid #ddd;&quot;&gt;NaN 대체값&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
        &lt;td style=&quot;padding:10px 16px; border:1px solid #ddd;&quot;&gt;&lt;code style=&quot;background:#eef2ff; color:#4f46e5; padding:2px 6px; border-radius:4px;&quot;&gt;posinf&lt;/code&gt;&lt;/td&gt;
        &lt;td style=&quot;padding:10px 16px; border:1px solid #ddd;&quot;&gt;시스템 최댓값&lt;/td&gt;
        &lt;td style=&quot;padding:10px 16px; border:1px solid #ddd;&quot;&gt;양의 무한대 대체값&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr style=&quot;background:#f9fafb;&quot;&gt;
        &lt;td style=&quot;padding:10px 16px; border:1px solid #ddd;&quot;&gt;&lt;code style=&quot;background:#eef2ff; color:#4f46e5; padding:2px 6px; border-radius:4px;&quot;&gt;neginf&lt;/code&gt;&lt;/td&gt;
        &lt;td style=&quot;padding:10px 16px; border:1px solid #ddd;&quot;&gt;시스템 최솟값&lt;/td&gt;
        &lt;td style=&quot;padding:10px 16px; border:1px solid #ddd;&quot;&gt;음의 무한대 대체값&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
        &lt;td style=&quot;padding:10px 16px; border:1px solid #ddd;&quot;&gt;&lt;code style=&quot;background:#eef2ff; color:#4f46e5; padding:2px 6px; border-radius:4px;&quot;&gt;copy&lt;/code&gt;&lt;/td&gt;
        &lt;td style=&quot;padding:10px 16px; border:1px solid #ddd;&quot;&gt;&lt;code style=&quot;background:#eef2ff; color:#4f46e5; padding:2px 6px; border-radius:4px;&quot;&gt;True&lt;/code&gt;&lt;/td&gt;
        &lt;td style=&quot;padding:10px 16px; border:1px solid #ddd;&quot;&gt;&lt;code style=&quot;background:#eef2ff; color:#4f46e5; padding:2px 6px; border-radius:4px;&quot;&gt;False&lt;/code&gt;면 원본 직접 수정&lt;/td&gt;
      &lt;/tr&gt;
    &lt;/tbody&gt;
  &lt;/table&gt;

  &lt;br/&gt;

  &lt;h2 style=&quot;font-size: 22px; font-weight: 700; color: #1a1a1a; margin-top: 40px; margin-bottom: 16px; position: sticky; top: 0; background: #eef4ff; border-left: 3px solid #4f8ef7; padding: 8px 14px; border-radius: 0 6px 6px 0; z-index: 10; box-shadow: 0 4px 16px rgba(0,0,0,0.08);&quot;&gt;작동 방식&lt;/h2&gt;

  &lt;h3 style=&quot;font-size: 18px; font-weight: 700; color: #1a1a1a; margin-top: 24px; margin-bottom: 12px;&quot;&gt;기본 동작&lt;/h3&gt;

  &lt;pre style=&quot;background:#1e1e2e; color:#cdd6f4; padding:20px 24px; border-radius:8px; font-size:14px; line-height:1.7; overflow-x:auto; margin-bottom:24px;&quot;&gt;&lt;code&gt;&lt;span style=&quot;color:#89b4fa;&quot;&gt;import&lt;/span&gt; numpy &lt;span style=&quot;color:#89b4fa;&quot;&gt;as&lt;/span&gt; np

a = np.array([1.0, np.nan, np.inf, -np.inf, 5.0])

np.nan_to_num(a)
&lt;span style=&quot;color:#6c7086;&quot;&gt;# array([ 1.00e+000,  0.00e+000,  1.80e+308, -1.80e+308,  5.00e+000])&lt;/span&gt;
&lt;span style=&quot;color:#6c7086;&quot;&gt;#              ↑ NaN→0      ↑ inf→최댓값    ↑ -inf→최솟값&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

  &lt;h3 style=&quot;font-size: 18px; font-weight: 700; color: #1a1a1a; margin-top: 24px; margin-bottom: 12px;&quot;&gt;대체값 직접 지정&lt;/h3&gt;

  &lt;pre style=&quot;background:#1e1e2e; color:#cdd6f4; padding:20px 24px; border-radius:8px; font-size:14px; line-height:1.7; overflow-x:auto; margin-bottom:24px;&quot;&gt;&lt;code&gt;np.nan_to_num(a, nan=0, posinf=999, neginf=-999)
&lt;span style=&quot;color:#6c7086;&quot;&gt;# array([  1.,   0., 999., -999.,   5.])&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

  &lt;h3 style=&quot;font-size: 18px; font-weight: 700; color: #1a1a1a; margin-top: 24px; margin-bottom: 12px;&quot;&gt;&lt;code style=&quot;background:#eef2ff; color:#4f46e5; padding:2px 8px; border-radius:4px; font-size:85%;&quot;&gt;copy=False&lt;/code&gt; — 원본 직접 수정&lt;/h3&gt;

  &lt;pre style=&quot;background:#1e1e2e; color:#cdd6f4; padding:20px 24px; border-radius:8px; font-size:14px; line-height:1.7; overflow-x:auto; margin-bottom:24px;&quot;&gt;&lt;code&gt;a = np.array([1.0, np.nan, np.inf])

np.nan_to_num(a, copy=False)   &lt;span style=&quot;color:#6c7086;&quot;&gt;# 원본 a가 바뀜&lt;/span&gt;
print(a)
&lt;span style=&quot;color:#6c7086;&quot;&gt;# array([1., 0., 1.8e+308])&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

  &lt;h3 style=&quot;font-size: 18px; font-weight: 700; color: #1a1a1a; margin-top: 24px; margin-bottom: 12px;&quot;&gt;스칼라 입력&lt;/h3&gt;

  &lt;pre style=&quot;background:#1e1e2e; color:#cdd6f4; padding:20px 24px; border-radius:8px; font-size:14px; line-height:1.7; overflow-x:auto; margin-bottom:24px;&quot;&gt;&lt;code&gt;np.nan_to_num(np.nan)           &lt;span style=&quot;color:#6c7086;&quot;&gt;# 0.0&lt;/span&gt;
np.nan_to_num(np.inf)           &lt;span style=&quot;color:#6c7086;&quot;&gt;# 1.7976931348623157e+308&lt;/span&gt;
np.nan_to_num(-np.inf)          &lt;span style=&quot;color:#6c7086;&quot;&gt;# -1.7976931348623157e+308&lt;/span&gt;

np.nan_to_num(np.nan, nan=-1)   &lt;span style=&quot;color:#6c7086;&quot;&gt;# -1.0&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

  &lt;br/&gt;

  &lt;h2 style=&quot;font-size: 22px; font-weight: 700; color: #1a1a1a; margin-top: 40px; margin-bottom: 16px; position: sticky; top: 0; background: #eef4ff; border-left: 3px solid #4f8ef7; padding: 8px 14px; border-radius: 0 6px 6px 0; z-index: 10; box-shadow: 0 4px 16px rgba(0,0,0,0.08);&quot;&gt;inf는 언제 생기나?&lt;/h2&gt;

  &lt;pre style=&quot;background:#1e1e2e; color:#cdd6f4; padding:20px 24px; border-radius:8px; font-size:14px; line-height:1.7; overflow-x:auto; margin-bottom:16px;&quot;&gt;&lt;code&gt;np.log(0)                                      &lt;span style=&quot;color:#6c7086;&quot;&gt;# -inf  ← 0의 로그&lt;/span&gt;
1 / np.array([0.0])                            &lt;span style=&quot;color:#6c7086;&quot;&gt;# inf   ← 0으로 나누기&lt;/span&gt;
np.array([np.inf]) - np.array([np.inf])        &lt;span style=&quot;color:#6c7086;&quot;&gt;# nan   ← inf - inf&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

  &lt;div style=&quot;background:#fffbeb; border-left:4px solid #f59e0b; padding:14px 18px; border-radius:0 8px 8px 0; margin-bottom:24px; font-size:15px;&quot;&gt;
    이런 연산 결과를 그대로 두면 이후 모든 계산이 오염됩니다. &lt;code style=&quot;background:#eef2ff; color:#4f46e5; padding:2px 6px; border-radius:4px;&quot;&gt;nan_to_num&lt;/code&gt;으로 선제 처리합니다.
  &lt;/div&gt;

  &lt;br/&gt;

  &lt;h2 style=&quot;font-size: 22px; font-weight: 700; color: #1a1a1a; margin-top: 40px; margin-bottom: 16px; position: sticky; top: 0; background: #eef4ff; border-left: 3px solid #4f8ef7; padding: 8px 14px; border-radius: 0 6px 6px 0; z-index: 10; box-shadow: 0 4px 16px rgba(0,0,0,0.08);&quot;&gt;응용 사례 1 — 로그 변환 후 정제&lt;/h2&gt;

  &lt;pre style=&quot;background:#1e1e2e; color:#cdd6f4; padding:20px 24px; border-radius:8px; font-size:14px; line-height:1.7; overflow-x:auto; margin-bottom:24px;&quot;&gt;&lt;code&gt;data = np.array([0.0, 1.0, 10.0, 100.0])

log_data = np.log(data)
&lt;span style=&quot;color:#6c7086;&quot;&gt;# array([-inf,  0.,  2.30,  4.60])   ← 0의 로그 = -inf&lt;/span&gt;

safe_log = np.nan_to_num(log_data, neginf=0)
&lt;span style=&quot;color:#6c7086;&quot;&gt;# array([0., 0., 2.30, 4.60])&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

  &lt;br/&gt;

  &lt;h2 style=&quot;font-size: 22px; font-weight: 700; color: #1a1a1a; margin-top: 40px; margin-bottom: 16px; position: sticky; top: 0; background: #eef4ff; border-left: 3px solid #4f8ef7; padding: 8px 14px; border-radius: 0 6px 6px 0; z-index: 10; box-shadow: 0 4px 16px rgba(0,0,0,0.08);&quot;&gt;응용 사례 2 — 비율 계산 (0으로 나누기 방지)&lt;/h2&gt;

  &lt;pre style=&quot;background:#1e1e2e; color:#cdd6f4; padding:20px 24px; border-radius:8px; font-size:14px; line-height:1.7; overflow-x:auto; margin-bottom:24px;&quot;&gt;&lt;code&gt;a = np.array([10.0, 0.0, 30.0])
b = np.array([2.0,  0.0,  5.0])

&lt;span style=&quot;color:#6c7086;&quot;&gt;# a/b → [5., nan, 6.]  (0/0=nan, x/0=inf 발생 가능)&lt;/span&gt;
ratio = np.nan_to_num(a / b, nan=0, posinf=0)
&lt;span style=&quot;color:#6c7086;&quot;&gt;# array([5., 0., 6.])&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

  &lt;br/&gt;

  &lt;h2 style=&quot;font-size: 22px; font-weight: 700; color: #1a1a1a; margin-top: 40px; margin-bottom: 16px; position: sticky; top: 0; background: #eef4ff; border-left: 3px solid #4f8ef7; padding: 8px 14px; border-radius: 0 6px 6px 0; z-index: 10; box-shadow: 0 4px 16px rgba(0,0,0,0.08);&quot;&gt;응용 사례 3 — 머신러닝 피처 전처리&lt;/h2&gt;

  &lt;pre style=&quot;background:#1e1e2e; color:#cdd6f4; padding:20px 24px; border-radius:8px; font-size:14px; line-height:1.7; overflow-x:auto; margin-bottom:16px;&quot;&gt;&lt;code&gt;features = np.array([[1.0, np.nan, 3.0],
                     [np.inf, 5.0, -np.inf],
                     [7.0, 8.0, 9.0]])

clean = np.nan_to_num(features, nan=0, posinf=0, neginf=0)
&lt;span style=&quot;color:#6c7086;&quot;&gt;# array([[1., 0., 3.],&lt;/span&gt;
&lt;span style=&quot;color:#6c7086;&quot;&gt;#        [0., 5., 0.],&lt;/span&gt;
&lt;span style=&quot;color:#6c7086;&quot;&gt;#        [7., 8., 9.]])&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

  &lt;p style=&quot;margin: 0 0 24px;&quot;&gt;모델에 NaN이나 inf가 들어가면 학습이 망가지기 때문에 전처리 단계에서 반드시 처리합니다.&lt;/p&gt;

  &lt;br/&gt;

  &lt;h2 style=&quot;font-size: 22px; font-weight: 700; color: #1a1a1a; margin-top: 40px; margin-bottom: 16px; position: sticky; top: 0; background: #eef4ff; border-left: 3px solid #4f8ef7; padding: 8px 14px; border-radius: 0 6px 6px 0; z-index: 10; box-shadow: 0 4px 16px rgba(0,0,0,0.08);&quot;&gt;응용 사례 4 — 코사인 유사도 행렬 정제&lt;/h2&gt;

  &lt;pre style=&quot;background:#1e1e2e; color:#cdd6f4; padding:20px 24px; border-radius:8px; font-size:14px; line-height:1.7; overflow-x:auto; margin-bottom:24px;&quot;&gt;&lt;code&gt;&lt;span style=&quot;color:#6c7086;&quot;&gt;# 벡터 크기가 0인 경우 코사인 유사도가 NaN이 될 수 있음&lt;/span&gt;
similarity = np.array([[1.0, np.nan, 0.8],
                       [np.nan, 1.0, 0.6],
                       [0.8, 0.6, 1.0]])

clean_sim = np.nan_to_num(similarity, nan=0.0)
&lt;span style=&quot;color:#6c7086;&quot;&gt;# NaN 위치를 0(유사도 없음)으로 처리&lt;/span&gt;
&lt;span style=&quot;color:#6c7086;&quot;&gt;# array([[1. , 0. , 0.8],&lt;/span&gt;
&lt;span style=&quot;color:#6c7086;&quot;&gt;#        [0. , 1. , 0.6],&lt;/span&gt;
&lt;span style=&quot;color:#6c7086;&quot;&gt;#        [0.8, 0.6, 1. ]])&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

  &lt;br/&gt;

  &lt;h2 style=&quot;font-size: 22px; font-weight: 700; color: #1a1a1a; margin-top: 40px; margin-bottom: 16px; position: sticky; top: 0; background: #eef4ff; border-left: 3px solid #4f8ef7; padding: 8px 14px; border-radius: 0 6px 6px 0; z-index: 10; box-shadow: 0 4px 16px rgba(0,0,0,0.08);&quot;&gt;&lt;code style=&quot;background:#eef2ff; color:#4f46e5; padding:2px 8px; border-radius:4px; font-size:85%;&quot;&gt;nan_to_num&lt;/code&gt; vs pandas &lt;code style=&quot;background:#eef2ff; color:#4f46e5; padding:2px 8px; border-radius:4px; font-size:85%;&quot;&gt;fillna&lt;/code&gt;&lt;/h2&gt;

  &lt;pre style=&quot;background:#1e1e2e; color:#cdd6f4; padding:20px 24px; border-radius:8px; font-size:14px; line-height:1.7; overflow-x:auto; margin-bottom:16px;&quot;&gt;&lt;code&gt;&lt;span style=&quot;color:#89b4fa;&quot;&gt;import&lt;/span&gt; pandas &lt;span style=&quot;color:#89b4fa;&quot;&gt;as&lt;/span&gt; pd

s = pd.Series([1.0, np.nan, np.inf])

s.fillna(0)
&lt;span style=&quot;color:#6c7086;&quot;&gt;# 0    1.0&lt;/span&gt;
&lt;span style=&quot;color:#6c7086;&quot;&gt;# 1    0.0&lt;/span&gt;
&lt;span style=&quot;color:#6c7086;&quot;&gt;# 2    inf   ← inf는 처리 안 됨&lt;/span&gt;

np.nan_to_num(s.values, nan=0, posinf=0)
&lt;span style=&quot;color:#6c7086;&quot;&gt;# array([1., 0., 0.])   ← NaN + inf 모두 처리&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

  &lt;table style=&quot;width:100%; border-collapse:collapse; margin-bottom:24px; font-size:15px;&quot;&gt;
    &lt;thead&gt;
      &lt;tr style=&quot;background:#4f8ef7; color:#fff;&quot;&gt;
        &lt;th style=&quot;padding:10px 16px; text-align:left; border:1px solid #ddd;&quot;&gt;&lt;/th&gt;
        &lt;th style=&quot;padding:10px 16px; text-align:left; border:1px solid #ddd;&quot;&gt;&lt;code style=&quot;background:rgba(255,255,255,0.2); padding:2px 6px; border-radius:4px; font-size:90%;&quot;&gt;nan_to_num&lt;/code&gt;&lt;/th&gt;
        &lt;th style=&quot;padding:10px 16px; text-align:left; border:1px solid #ddd;&quot;&gt;pandas &lt;code style=&quot;background:rgba(255,255,255,0.2); padding:2px 6px; border-radius:4px; font-size:90%;&quot;&gt;fillna&lt;/code&gt;&lt;/th&gt;
      &lt;/tr&gt;
    &lt;/thead&gt;
    &lt;tbody&gt;
      &lt;tr style=&quot;background:#f9fafb;&quot;&gt;
        &lt;td style=&quot;padding:10px 16px; border:1px solid #ddd;&quot;&gt;NaN 처리&lt;/td&gt;
        &lt;td style=&quot;padding:10px 16px; border:1px solid #ddd;&quot;&gt;O&lt;/td&gt;
        &lt;td style=&quot;padding:10px 16px; border:1px solid #ddd;&quot;&gt;O&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
        &lt;td style=&quot;padding:10px 16px; border:1px solid #ddd;&quot;&gt;inf 처리&lt;/td&gt;
        &lt;td style=&quot;padding:10px 16px; border:1px solid #ddd;&quot;&gt;O&lt;/td&gt;
        &lt;td style=&quot;padding:10px 16px; border:1px solid #ddd;&quot;&gt;X&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr style=&quot;background:#f9fafb;&quot;&gt;
        &lt;td style=&quot;padding:10px 16px; border:1px solid #ddd;&quot;&gt;대상&lt;/td&gt;
        &lt;td style=&quot;padding:10px 16px; border:1px solid #ddd;&quot;&gt;NumPy 배열&lt;/td&gt;
        &lt;td style=&quot;padding:10px 16px; border:1px solid #ddd;&quot;&gt;pandas Series / DataFrame&lt;/td&gt;
      &lt;/tr&gt;
    &lt;/tbody&gt;
  &lt;/table&gt;

  &lt;div style=&quot;background:#fffbeb; border-left:4px solid #f59e0b; padding:14px 18px; border-radius:0 8px 8px 0; margin-bottom:24px; font-size:15px;&quot;&gt;
    &lt;strong&gt;핵심 직관:&lt;/strong&gt; &lt;code style=&quot;background:#eef2ff; color:#4f46e5; padding:2px 6px; border-radius:4px;&quot;&gt;nan_to_num&lt;/code&gt;은 배열을 수치 연산에 &quot;안전한 상태&quot;로 만드는 안전망입니다. 로그·나눗셈 등 inf가 생길 수 있는 연산 직후, 또는 모델 입력 직전에 씁니다.
  &lt;/div&gt;

&lt;/div&gt;</description>
      <category>Python/Python numpy</category>
      <category>nan_to_num</category>
      <category>numpy</category>
      <category>Python</category>
      <author>CosmosProject</author>
      <guid isPermaLink="true">https://cosmosproject.tistory.com/943</guid>
      <comments>https://cosmosproject.tistory.com/943#entry943comment</comments>
      <pubDate>Wed, 20 May 2026 21:59:11 +0900</pubDate>
    </item>
    <item>
      <title>Python Pandas : map</title>
      <link>https://cosmosproject.tistory.com/942</link>
      <description>&lt;div style=&quot;font-family: 'Noto Sans KR', Apple SD Gothic Neo, sans-serif; font-size: 16px; line-height: 1.8; color: #333; max-width: 780px; margin: 0 auto;&quot;&gt;

  &lt;h1 style=&quot;font-size: 28px; font-weight: 700; color: #1a1a1a; border-bottom: 3px solid #4f8ef7; padding-bottom: 12px; margin-bottom: 32px;&quot;&gt;Pandas &lt;code style=&quot;background:#eef2ff; color:#4f46e5; padding:2px 8px; border-radius:4px; font-size:90%;&quot;&gt;map()&lt;/code&gt; — 딕셔너리·함수·Series로 값을 1:1 변환하는 메서드&lt;/h1&gt;

  &lt;p style=&quot;margin: 0 0 24px;&quot;&gt;&lt;code style=&quot;background:#eef2ff; color:#4f46e5; padding:2px 8px; border-radius:4px; font-size:90%;&quot;&gt;map()&lt;/code&gt;은 Series의 각 값을 다른 값으로 변환하는 메서드입니다. 딕셔너리, 함수, 또는 다른 Series를 기준으로 element-wise 변환하며, 코드값을 레이블로 바꾸거나 외부 테이블 기준으로 값을 붙이는 작업에 가장 자주 씁니다.&lt;/p&gt;

  &lt;div style=&quot;background:#fffbeb; border-left:4px solid #f59e0b; padding:14px 18px; border-radius:0 8px 8px 0; margin-bottom:32px; font-size:15px;&quot;&gt;
    &lt;strong&gt;주의:&lt;/strong&gt; &lt;code style=&quot;background:#eef2ff; color:#4f46e5; padding:2px 6px; border-radius:4px;&quot;&gt;map()&lt;/code&gt;은 &lt;strong&gt;Series 전용&lt;/strong&gt;입니다. DataFrame 전체에 적용하려면 &lt;code style=&quot;background:#eef2ff; color:#4f46e5; padding:2px 6px; border-radius:4px;&quot;&gt;apply()&lt;/code&gt; 또는 &lt;code style=&quot;background:#eef2ff; color:#4f46e5; padding:2px 6px; border-radius:4px;&quot;&gt;map()&lt;/code&gt;(pandas 2.1+)을 씁니다.
  &lt;/div&gt;

  &lt;h2 style=&quot;font-size: 22px; font-weight: 700; color: #1a1a1a; margin-top: 40px; margin-bottom: 16px; position: sticky; top: 0; background: #eef4ff; border-left: 3px solid #4f8ef7; padding: 8px 14px; border-radius: 0 6px 6px 0; z-index: 10; box-shadow: 0 4px 16px rgba(0,0,0,0.08);&quot;&gt;기본 문법&lt;/h2&gt;

  &lt;pre style=&quot;background:#1e1e2e; color:#cdd6f4; padding:20px 24px; border-radius:8px; font-size:14px; line-height:1.7; overflow-x:auto; margin-bottom:16px;&quot;&gt;&lt;code&gt;Series.map(arg, na_action=None)&lt;/code&gt;&lt;/pre&gt;

  &lt;table style=&quot;width:100%; border-collapse:collapse; margin-bottom:24px; font-size:15px;&quot;&gt;
    &lt;thead&gt;
      &lt;tr style=&quot;background:#4f8ef7; color:#fff;&quot;&gt;
        &lt;th style=&quot;padding:10px 16px; text-align:left; border:1px solid #ddd;&quot;&gt;파라미터&lt;/th&gt;
        &lt;th style=&quot;padding:10px 16px; text-align:left; border:1px solid #ddd;&quot;&gt;설명&lt;/th&gt;
      &lt;/tr&gt;
    &lt;/thead&gt;
    &lt;tbody&gt;
      &lt;tr style=&quot;background:#f9fafb;&quot;&gt;
        &lt;td style=&quot;padding:10px 16px; border:1px solid #ddd;&quot;&gt;&lt;code style=&quot;background:#eef2ff; color:#4f46e5; padding:2px 6px; border-radius:4px;&quot;&gt;arg&lt;/code&gt;&lt;/td&gt;
        &lt;td style=&quot;padding:10px 16px; border:1px solid #ddd;&quot;&gt;딕셔너리, 함수, Series 중 하나&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
        &lt;td style=&quot;padding:10px 16px; border:1px solid #ddd;&quot;&gt;&lt;code style=&quot;background:#eef2ff; color:#4f46e5; padding:2px 6px; border-radius:4px;&quot;&gt;na_action&lt;/code&gt;&lt;/td&gt;
        &lt;td style=&quot;padding:10px 16px; border:1px solid #ddd;&quot;&gt;&lt;code style=&quot;background:#eef2ff; color:#4f46e5; padding:2px 6px; border-radius:4px;&quot;&gt;None&lt;/code&gt;(기본) 또는 &lt;code style=&quot;background:#eef2ff; color:#4f46e5; padding:2px 6px; border-radius:4px;&quot;&gt;'ignore'&lt;/code&gt; — NaN을 변환하지 않고 그대로 유지&lt;/td&gt;
      &lt;/tr&gt;
    &lt;/tbody&gt;
  &lt;/table&gt;

  &lt;br/&gt;

  &lt;h2 style=&quot;font-size: 22px; font-weight: 700; color: #1a1a1a; margin-top: 40px; margin-bottom: 16px; position: sticky; top: 0; background: #eef4ff; border-left: 3px solid #4f8ef7; padding: 8px 14px; border-radius: 0 6px 6px 0; z-index: 10; box-shadow: 0 4px 16px rgba(0,0,0,0.08);&quot;&gt;1. 딕셔너리로 값 치환&lt;/h2&gt;

  &lt;p style=&quot;margin: 0 0 16px;&quot;&gt;딕셔너리에 없는 값은 자동으로 &lt;code style=&quot;background:#eef2ff; color:#4f46e5; padding:2px 8px; border-radius:4px; font-size:90%;&quot;&gt;NaN&lt;/code&gt;이 됩니다.&lt;/p&gt;

  &lt;pre style=&quot;background:#1e1e2e; color:#cdd6f4; padding:20px 24px; border-radius:8px; font-size:14px; line-height:1.7; overflow-x:auto; margin-bottom:16px;&quot;&gt;&lt;code&gt;&lt;span style=&quot;color:#89b4fa;&quot;&gt;import&lt;/span&gt; pandas &lt;span style=&quot;color:#89b4fa;&quot;&gt;as&lt;/span&gt; pd

grade = pd.Series(['A', 'B', 'C', 'A', 'B'])

grade.map({'A': '우수', 'B': '보통', 'C': '미흡'})
&lt;span style=&quot;color:#6c7086;&quot;&gt;# 0    우수&lt;/span&gt;
&lt;span style=&quot;color:#6c7086;&quot;&gt;# 1    보통&lt;/span&gt;
&lt;span style=&quot;color:#6c7086;&quot;&gt;# 2    미흡&lt;/span&gt;
&lt;span style=&quot;color:#6c7086;&quot;&gt;# 3    우수&lt;/span&gt;
&lt;span style=&quot;color:#6c7086;&quot;&gt;# 4    보통&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

  &lt;pre style=&quot;background:#1e1e2e; color:#cdd6f4; padding:20px 24px; border-radius:8px; font-size:14px; line-height:1.7; overflow-x:auto; margin-bottom:24px;&quot;&gt;&lt;code&gt;grade2 = pd.Series(['A', 'B', 'D'])   &lt;span style=&quot;color:#6c7086;&quot;&gt;# 'D'는 딕셔너리에 없음&lt;/span&gt;
grade2.map({'A': '우수', 'B': '보통', 'C': '미흡'})
&lt;span style=&quot;color:#6c7086;&quot;&gt;# 0     우수&lt;/span&gt;
&lt;span style=&quot;color:#6c7086;&quot;&gt;# 1     보통&lt;/span&gt;
&lt;span style=&quot;color:#6c7086;&quot;&gt;# 2     NaN   ← 매핑 없으면 NaN&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

  &lt;br/&gt;

  &lt;h2 style=&quot;font-size: 22px; font-weight: 700; color: #1a1a1a; margin-top: 40px; margin-bottom: 16px; position: sticky; top: 0; background: #eef4ff; border-left: 3px solid #4f8ef7; padding: 8px 14px; border-radius: 0 6px 6px 0; z-index: 10; box-shadow: 0 4px 16px rgba(0,0,0,0.08);&quot;&gt;2. 함수로 변환&lt;/h2&gt;

  &lt;pre style=&quot;background:#1e1e2e; color:#cdd6f4; padding:20px 24px; border-radius:8px; font-size:14px; line-height:1.7; overflow-x:auto; margin-bottom:16px;&quot;&gt;&lt;code&gt;prices = pd.Series([1000, 2500, 800, 3200])

prices.map(&lt;span style=&quot;color:#89b4fa;&quot;&gt;lambda&lt;/span&gt; x: x * 1.1)   &lt;span style=&quot;color:#6c7086;&quot;&gt;# 10% 인상&lt;/span&gt;
&lt;span style=&quot;color:#6c7086;&quot;&gt;# 0    1100.0&lt;/span&gt;
&lt;span style=&quot;color:#6c7086;&quot;&gt;# 1    2750.0&lt;/span&gt;
&lt;span style=&quot;color:#6c7086;&quot;&gt;# 2     880.0&lt;/span&gt;
&lt;span style=&quot;color:#6c7086;&quot;&gt;# 3    3520.0&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

  &lt;pre style=&quot;background:#1e1e2e; color:#cdd6f4; padding:20px 24px; border-radius:8px; font-size:14px; line-height:1.7; overflow-x:auto; margin-bottom:24px;&quot;&gt;&lt;code&gt;&lt;span style=&quot;color:#89b4fa;&quot;&gt;def&lt;/span&gt; grade_label(score):
    &lt;span style=&quot;color:#89b4fa;&quot;&gt;if&lt;/span&gt;   score &gt;= 90: &lt;span style=&quot;color:#89b4fa;&quot;&gt;return&lt;/span&gt; 'A'
    &lt;span style=&quot;color:#89b4fa;&quot;&gt;elif&lt;/span&gt; score &gt;= 80: &lt;span style=&quot;color:#89b4fa;&quot;&gt;return&lt;/span&gt; 'B'
    &lt;span style=&quot;color:#89b4fa;&quot;&gt;else&lt;/span&gt;:             &lt;span style=&quot;color:#89b4fa;&quot;&gt;return&lt;/span&gt; 'C'

scores = pd.Series([95, 82, 74, 88])
scores.map(grade_label)
&lt;span style=&quot;color:#6c7086;&quot;&gt;# 0    A&lt;/span&gt;
&lt;span style=&quot;color:#6c7086;&quot;&gt;# 1    B&lt;/span&gt;
&lt;span style=&quot;color:#6c7086;&quot;&gt;# 2    C&lt;/span&gt;
&lt;span style=&quot;color:#6c7086;&quot;&gt;# 3    B&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

  &lt;br/&gt;

  &lt;h2 style=&quot;font-size: 22px; font-weight: 700; color: #1a1a1a; margin-top: 40px; margin-bottom: 16px; position: sticky; top: 0; background: #eef4ff; border-left: 3px solid #4f8ef7; padding: 8px 14px; border-radius: 0 6px 6px 0; z-index: 10; box-shadow: 0 4px 16px rgba(0,0,0,0.08);&quot;&gt;3. Series로 변환 (인덱스 기준 매핑)&lt;/h2&gt;

  &lt;pre style=&quot;background:#1e1e2e; color:#cdd6f4; padding:20px 24px; border-radius:8px; font-size:14px; line-height:1.7; overflow-x:auto; margin-bottom:24px;&quot;&gt;&lt;code&gt;code    = pd.Series(['KR', 'US', 'JP', 'KR'])
mapping = pd.Series({'KR': '한국', 'US': '미국', 'JP': '일본'})

code.map(mapping)
&lt;span style=&quot;color:#6c7086;&quot;&gt;# 0    한국&lt;/span&gt;
&lt;span style=&quot;color:#6c7086;&quot;&gt;# 1    미국&lt;/span&gt;
&lt;span style=&quot;color:#6c7086;&quot;&gt;# 2    일본&lt;/span&gt;
&lt;span style=&quot;color:#6c7086;&quot;&gt;# 3    한국&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

  &lt;br/&gt;

  &lt;h2 style=&quot;font-size: 22px; font-weight: 700; color: #1a1a1a; margin-top: 40px; margin-bottom: 16px; position: sticky; top: 0; background: #eef4ff; border-left: 3px solid #4f8ef7; padding: 8px 14px; border-radius: 0 6px 6px 0; z-index: 10; box-shadow: 0 4px 16px rgba(0,0,0,0.08);&quot;&gt;4. &lt;code style=&quot;background:#eef2ff; color:#4f46e5; padding:2px 8px; border-radius:4px; font-size:85%;&quot;&gt;na_action='ignore'&lt;/code&gt; — NaN 건드리지 않기&lt;/h2&gt;

  &lt;pre style=&quot;background:#1e1e2e; color:#cdd6f4; padding:20px 24px; border-radius:8px; font-size:14px; line-height:1.7; overflow-x:auto; margin-bottom:24px;&quot;&gt;&lt;code&gt;s = pd.Series(['A', None, 'B'])

s.map({'A': 1, 'B': 2})
&lt;span style=&quot;color:#6c7086;&quot;&gt;# 0    1.0&lt;/span&gt;
&lt;span style=&quot;color:#6c7086;&quot;&gt;# 1    NaN   ← None도 변환 시도 → NaN&lt;/span&gt;
&lt;span style=&quot;color:#6c7086;&quot;&gt;# 2    2.0&lt;/span&gt;

s.map({'A': 1, 'B': 2}, na_action='ignore')
&lt;span style=&quot;color:#6c7086;&quot;&gt;# 0       1&lt;/span&gt;
&lt;span style=&quot;color:#6c7086;&quot;&gt;# 1    None   ← None 그대로 유지&lt;/span&gt;
&lt;span style=&quot;color:#6c7086;&quot;&gt;# 2       2&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

  &lt;br/&gt;

  &lt;h2 style=&quot;font-size: 22px; font-weight: 700; color: #1a1a1a; margin-top: 40px; margin-bottom: 16px; position: sticky; top: 0; background: #eef4ff; border-left: 3px solid #4f8ef7; padding: 8px 14px; border-radius: 0 6px 6px 0; z-index: 10; box-shadow: 0 4px 16px rgba(0,0,0,0.08);&quot;&gt;&lt;code style=&quot;background:#eef2ff; color:#4f46e5; padding:2px 8px; border-radius:4px; font-size:85%;&quot;&gt;map&lt;/code&gt; vs &lt;code style=&quot;background:#eef2ff; color:#4f46e5; padding:2px 8px; border-radius:4px; font-size:85%;&quot;&gt;apply&lt;/code&gt; vs &lt;code style=&quot;background:#eef2ff; color:#4f46e5; padding:2px 8px; border-radius:4px; font-size:85%;&quot;&gt;replace&lt;/code&gt;&lt;/h2&gt;

  &lt;table style=&quot;width:100%; border-collapse:collapse; margin-bottom:16px; font-size:15px;&quot;&gt;
    &lt;thead&gt;
      &lt;tr style=&quot;background:#4f8ef7; color:#fff;&quot;&gt;
        &lt;th style=&quot;padding:10px 16px; text-align:left; border:1px solid #ddd;&quot;&gt;&lt;/th&gt;
        &lt;th style=&quot;padding:10px 16px; text-align:left; border:1px solid #ddd;&quot;&gt;&lt;code style=&quot;background:rgba(255,255,255,0.2); padding:2px 6px; border-radius:4px; font-size:90%;&quot;&gt;map&lt;/code&gt;&lt;/th&gt;
        &lt;th style=&quot;padding:10px 16px; text-align:left; border:1px solid #ddd;&quot;&gt;&lt;code style=&quot;background:rgba(255,255,255,0.2); padding:2px 6px; border-radius:4px; font-size:90%;&quot;&gt;apply&lt;/code&gt;&lt;/th&gt;
        &lt;th style=&quot;padding:10px 16px; text-align:left; border:1px solid #ddd;&quot;&gt;&lt;code style=&quot;background:rgba(255,255,255,0.2); padding:2px 6px; border-radius:4px; font-size:90%;&quot;&gt;replace&lt;/code&gt;&lt;/th&gt;
      &lt;/tr&gt;
    &lt;/thead&gt;
    &lt;tbody&gt;
      &lt;tr style=&quot;background:#f9fafb;&quot;&gt;
        &lt;td style=&quot;padding:10px 16px; border:1px solid #ddd;&quot;&gt;대상&lt;/td&gt;
        &lt;td style=&quot;padding:10px 16px; border:1px solid #ddd;&quot;&gt;Series&lt;/td&gt;
        &lt;td style=&quot;padding:10px 16px; border:1px solid #ddd;&quot;&gt;Series, DataFrame&lt;/td&gt;
        &lt;td style=&quot;padding:10px 16px; border:1px solid #ddd;&quot;&gt;Series, DataFrame&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
        &lt;td style=&quot;padding:10px 16px; border:1px solid #ddd;&quot;&gt;딕셔너리 미매핑&lt;/td&gt;
        &lt;td style=&quot;padding:10px 16px; border:1px solid #ddd;&quot;&gt;NaN&lt;/td&gt;
        &lt;td style=&quot;padding:10px 16px; border:1px solid #ddd;&quot;&gt;—&lt;/td&gt;
        &lt;td style=&quot;padding:10px 16px; border:1px solid #ddd;&quot;&gt;원래 값 유지&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr style=&quot;background:#f9fafb;&quot;&gt;
        &lt;td style=&quot;padding:10px 16px; border:1px solid #ddd;&quot;&gt;주 용도&lt;/td&gt;
        &lt;td style=&quot;padding:10px 16px; border:1px solid #ddd;&quot;&gt;값 치환/변환&lt;/td&gt;
        &lt;td style=&quot;padding:10px 16px; border:1px solid #ddd;&quot;&gt;복잡한 연산&lt;/td&gt;
        &lt;td style=&quot;padding:10px 16px; border:1px solid #ddd;&quot;&gt;특정 값만 바꾸기&lt;/td&gt;
      &lt;/tr&gt;
    &lt;/tbody&gt;
  &lt;/table&gt;

  &lt;pre style=&quot;background:#1e1e2e; color:#cdd6f4; padding:20px 24px; border-radius:8px; font-size:14px; line-height:1.7; overflow-x:auto; margin-bottom:24px;&quot;&gt;&lt;code&gt;s = pd.Series([1, 2, 3])

s.map({1: 'a', 2: 'b'})      &lt;span style=&quot;color:#6c7086;&quot;&gt;# 3 → NaN&lt;/span&gt;
s.replace({1: 'a', 2: 'b'})  &lt;span style=&quot;color:#6c7086;&quot;&gt;# 3 → 3 (그대로)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

  &lt;br/&gt;

  &lt;h2 style=&quot;font-size: 22px; font-weight: 700; color: #1a1a1a; margin-top: 40px; margin-bottom: 16px; position: sticky; top: 0; background: #eef4ff; border-left: 3px solid #4f8ef7; padding: 8px 14px; border-radius: 0 6px 6px 0; z-index: 10; box-shadow: 0 4px 16px rgba(0,0,0,0.08);&quot;&gt;응용 사례 1 — 코드값을 레이블로 변환&lt;/h2&gt;

  &lt;pre style=&quot;background:#1e1e2e; color:#cdd6f4; padding:20px 24px; border-radius:8px; font-size:14px; line-height:1.7; overflow-x:auto; margin-bottom:24px;&quot;&gt;&lt;code&gt;df = pd.DataFrame({
    '이름':   ['철수', '영희', '민준'],
    '성별코드': [1, 2, 1],
    '등급코드': ['A', 'C', 'B']
})

df['성별'] = df['성별코드'].map({1: '남', 2: '여'})
df['등급'] = df['등급코드'].map({'A': '우수', 'B': '보통', 'C': '미흡'})
&lt;span style=&quot;color:#6c7086;&quot;&gt;#    이름  성별코드 등급코드  성별  등급&lt;/span&gt;
&lt;span style=&quot;color:#6c7086;&quot;&gt;# 0  철수     1    A      남   우수&lt;/span&gt;
&lt;span style=&quot;color:#6c7086;&quot;&gt;# 1  영희     2    C      여   미흡&lt;/span&gt;
&lt;span style=&quot;color:#6c7086;&quot;&gt;# 2  민준     1    B      남   보통&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

  &lt;br/&gt;

  &lt;h2 style=&quot;font-size: 22px; font-weight: 700; color: #1a1a1a; margin-top: 40px; margin-bottom: 16px; position: sticky; top: 0; background: #eef4ff; border-left: 3px solid #4f8ef7; padding: 8px 14px; border-radius: 0 6px 6px 0; z-index: 10; box-shadow: 0 4px 16px rgba(0,0,0,0.08);&quot;&gt;응용 사례 2 — 구간별 레이블 (점수 → 등급)&lt;/h2&gt;

  &lt;pre style=&quot;background:#1e1e2e; color:#cdd6f4; padding:20px 24px; border-radius:8px; font-size:14px; line-height:1.7; overflow-x:auto; margin-bottom:24px;&quot;&gt;&lt;code&gt;scores = pd.Series([95, 82, 74, 61, 88])

&lt;span style=&quot;color:#89b4fa;&quot;&gt;def&lt;/span&gt; to_grade(score):
    &lt;span style=&quot;color:#89b4fa;&quot;&gt;if&lt;/span&gt;   score &gt;= 90: &lt;span style=&quot;color:#89b4fa;&quot;&gt;return&lt;/span&gt; 'A'
    &lt;span style=&quot;color:#89b4fa;&quot;&gt;elif&lt;/span&gt; score &gt;= 80: &lt;span style=&quot;color:#89b4fa;&quot;&gt;return&lt;/span&gt; 'B'
    &lt;span style=&quot;color:#89b4fa;&quot;&gt;elif&lt;/span&gt; score &gt;= 70: &lt;span style=&quot;color:#89b4fa;&quot;&gt;return&lt;/span&gt; 'C'
    &lt;span style=&quot;color:#89b4fa;&quot;&gt;else&lt;/span&gt;:             &lt;span style=&quot;color:#89b4fa;&quot;&gt;return&lt;/span&gt; 'D'

scores.map(to_grade)
&lt;span style=&quot;color:#6c7086;&quot;&gt;# 0    A&lt;/span&gt;
&lt;span style=&quot;color:#6c7086;&quot;&gt;# 1    B&lt;/span&gt;
&lt;span style=&quot;color:#6c7086;&quot;&gt;# 2    C&lt;/span&gt;
&lt;span style=&quot;color:#6c7086;&quot;&gt;# 3    D&lt;/span&gt;
&lt;span style=&quot;color:#6c7086;&quot;&gt;# 4    B&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

  &lt;br/&gt;

  &lt;h2 style=&quot;font-size: 22px; font-weight: 700; color: #1a1a1a; margin-top: 40px; margin-bottom: 16px; position: sticky; top: 0; background: #eef4ff; border-left: 3px solid #4f8ef7; padding: 8px 14px; border-radius: 0 6px 6px 0; z-index: 10; box-shadow: 0 4px 16px rgba(0,0,0,0.08);&quot;&gt;응용 사례 3 — 외부 테이블과 조인 대용&lt;/h2&gt;

  &lt;pre style=&quot;background:#1e1e2e; color:#cdd6f4; padding:20px 24px; border-radius:8px; font-size:14px; line-height:1.7; overflow-x:auto; margin-bottom:24px;&quot;&gt;&lt;code&gt;&lt;span style=&quot;color:#6c7086;&quot;&gt;# 지역코드 테이블&lt;/span&gt;
region_name = pd.Series({'SEL': '서울', 'PUS': '부산', 'ICN': '인천'})

orders = pd.DataFrame({
    '주문번호': [1, 2, 3, 4],
    '지역코드': ['SEL', 'PUS', 'SEL', 'ICN']
})

orders['지역명'] = orders['지역코드'].map(region_name)
&lt;span style=&quot;color:#6c7086;&quot;&gt;#    주문번호 지역코드  지역명&lt;/span&gt;
&lt;span style=&quot;color:#6c7086;&quot;&gt;# 0     1    SEL    서울&lt;/span&gt;
&lt;span style=&quot;color:#6c7086;&quot;&gt;# 1     2    PUS    부산&lt;/span&gt;
&lt;span style=&quot;color:#6c7086;&quot;&gt;# 2     3    SEL    서울&lt;/span&gt;
&lt;span style=&quot;color:#6c7086;&quot;&gt;# 3     4    ICN    인천&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

  &lt;br/&gt;

  &lt;h2 style=&quot;font-size: 22px; font-weight: 700; color: #1a1a1a; margin-top: 40px; margin-bottom: 16px; position: sticky; top: 0; background: #eef4ff; border-left: 3px solid #4f8ef7; padding: 8px 14px; border-radius: 0 6px 6px 0; z-index: 10; box-shadow: 0 4px 16px rgba(0,0,0,0.08);&quot;&gt;응용 사례 4 — 문자열 전처리&lt;/h2&gt;

  &lt;pre style=&quot;background:#1e1e2e; color:#cdd6f4; padding:20px 24px; border-radius:8px; font-size:14px; line-height:1.7; overflow-x:auto; margin-bottom:24px;&quot;&gt;&lt;code&gt;raw = pd.Series(['  서울 ', 'BUSAN', '인 천'])

raw.map(str.strip)                       &lt;span style=&quot;color:#6c7086;&quot;&gt;# 앞뒤 공백 제거&lt;/span&gt;
raw.map(str.lower)                       &lt;span style=&quot;color:#6c7086;&quot;&gt;# 소문자 변환&lt;/span&gt;
raw.map(&lt;span style=&quot;color:#89b4fa;&quot;&gt;lambda&lt;/span&gt; x: x.replace(' ', ''))   &lt;span style=&quot;color:#6c7086;&quot;&gt;# 내부 공백 제거&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

  &lt;br/&gt;

  &lt;h2 style=&quot;font-size: 22px; font-weight: 700; color: #1a1a1a; margin-top: 40px; margin-bottom: 16px; position: sticky; top: 0; background: #eef4ff; border-left: 3px solid #4f8ef7; padding: 8px 14px; border-radius: 0 6px 6px 0; z-index: 10; box-shadow: 0 4px 16px rgba(0,0,0,0.08);&quot;&gt;핵심 요약&lt;/h2&gt;

  &lt;table style=&quot;width:100%; border-collapse:collapse; margin-bottom:24px; font-size:15px;&quot;&gt;
    &lt;thead&gt;
      &lt;tr style=&quot;background:#4f8ef7; color:#fff;&quot;&gt;
        &lt;th style=&quot;padding:10px 16px; text-align:left; border:1px solid #ddd;&quot;&gt;&lt;/th&gt;
        &lt;th style=&quot;padding:10px 16px; text-align:left; border:1px solid #ddd;&quot;&gt;내용&lt;/th&gt;
      &lt;/tr&gt;
    &lt;/thead&gt;
    &lt;tbody&gt;
      &lt;tr style=&quot;background:#f9fafb;&quot;&gt;
        &lt;td style=&quot;padding:10px 16px; border:1px solid #ddd;&quot;&gt;대상&lt;/td&gt;
        &lt;td style=&quot;padding:10px 16px; border:1px solid #ddd;&quot;&gt;Series 전용&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
        &lt;td style=&quot;padding:10px 16px; border:1px solid #ddd;&quot;&gt;입력 형태&lt;/td&gt;
        &lt;td style=&quot;padding:10px 16px; border:1px solid #ddd;&quot;&gt;딕셔너리, 함수, Series&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr style=&quot;background:#f9fafb;&quot;&gt;
        &lt;td style=&quot;padding:10px 16px; border:1px solid #ddd;&quot;&gt;미매핑 값&lt;/td&gt;
        &lt;td style=&quot;padding:10px 16px; border:1px solid #ddd;&quot;&gt;&lt;code style=&quot;background:#eef2ff; color:#4f46e5; padding:2px 6px; border-radius:4px;&quot;&gt;NaN&lt;/code&gt; (딕셔너리 사용 시)&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
        &lt;td style=&quot;padding:10px 16px; border:1px solid #ddd;&quot;&gt;&lt;code style=&quot;background:#eef2ff; color:#4f46e5; padding:2px 6px; border-radius:4px;&quot;&gt;na_action='ignore'&lt;/code&gt;&lt;/td&gt;
        &lt;td style=&quot;padding:10px 16px; border:1px solid #ddd;&quot;&gt;NaN은 변환 없이 유지&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr style=&quot;background:#f9fafb;&quot;&gt;
        &lt;td style=&quot;padding:10px 16px; border:1px solid #ddd;&quot;&gt;vs &lt;code style=&quot;background:#eef2ff; color:#4f46e5; padding:2px 6px; border-radius:4px;&quot;&gt;replace&lt;/code&gt;&lt;/td&gt;
        &lt;td style=&quot;padding:10px 16px; border:1px solid #ddd;&quot;&gt;미매핑 시 NaN 반환 vs 원래 값 유지&lt;/td&gt;
      &lt;/tr&gt;
    &lt;/tbody&gt;
  &lt;/table&gt;

  &lt;div style=&quot;background:#fffbeb; border-left:4px solid #f59e0b; padding:14px 18px; border-radius:0 8px 8px 0; margin-bottom:24px; font-size:15px;&quot;&gt;
    &lt;strong&gt;핵심 직관:&lt;/strong&gt; &lt;code style=&quot;background:#eef2ff; color:#4f46e5; padding:2px 6px; border-radius:4px;&quot;&gt;map&lt;/code&gt;은 &quot;값 → 값&quot; 1:1 변환 전용입니다. 코드값을 사람이 읽기 좋은 레이블로 바꾸거나, 외부 테이블 기준으로 값을 붙이는 작업에 가장 자주 씁니다.
  &lt;/div&gt;

&lt;/div&gt;</description>
      <category>Python/Python Pandas</category>
      <category>map</category>
      <category>PANDAS</category>
      <category>Python</category>
      <author>CosmosProject</author>
      <guid isPermaLink="true">https://cosmosproject.tistory.com/942</guid>
      <comments>https://cosmosproject.tistory.com/942#entry942comment</comments>
      <pubDate>Wed, 20 May 2026 21:54:14 +0900</pubDate>
    </item>
    <item>
      <title>Python Pandas : agg (열마다 다른 집계함수 적용)</title>
      <link>https://cosmosproject.tistory.com/941</link>
      <description>&lt;div style=&quot;font-family: 'Noto Sans KR', Apple SD Gothic Neo, sans-serif; font-size: 16px; line-height: 1.8; color: #333; max-width: 780px; margin: 0 auto;&quot;&gt;

  &lt;h1 style=&quot;font-size: 28px; font-weight: 700; color: #1a1a1a; border-bottom: 3px solid #4f8ef7; padding-bottom: 12px; margin-bottom: 32px;&quot;&gt;Pandas &lt;code style=&quot;background:#eef2ff; color:#4f46e5; padding:2px 8px; border-radius:4px; font-size:90%;&quot;&gt;agg()&lt;/code&gt; — 열마다 다른 집계 함수를 한 번에 적용하는 메서드&lt;/h1&gt;

  &lt;p style=&quot;margin: 0 0 24px;&quot;&gt;&lt;code style=&quot;background:#eef2ff; color:#4f46e5; padding:2px 8px; border-radius:4px; font-size:90%;&quot;&gt;agg()&lt;/code&gt;는 DataFrame이나 GroupBy 객체에 집계 함수를 적용하는 메서드입니다. 단일 함수는 물론, 열마다 다른 함수를 적용하거나 여러 함수를 한 번에 실행할 수 있습니다.&lt;/p&gt;

  &lt;h2 style=&quot;font-size: 22px; font-weight: 700; color: #1a1a1a; margin-top: 40px; margin-bottom: 16px;&quot;&gt;기본 문법&lt;/h2&gt;

  &lt;pre style=&quot;background:#1e1e2e; color:#cdd6f4; padding:20px 24px; border-radius:8px; font-size:14px; line-height:1.7; overflow-x:auto; margin-bottom:16px;&quot;&gt;&lt;code&gt;DataFrame.agg(func, axis=0)
GroupBy.agg(func)&lt;/code&gt;&lt;/pre&gt;

  &lt;table style=&quot;width:100%; border-collapse:collapse; margin-bottom:24px; font-size:15px;&quot;&gt;
    &lt;thead&gt;
      &lt;tr style=&quot;background:#4f8ef7; color:#fff;&quot;&gt;
        &lt;th style=&quot;padding:10px 16px; text-align:left; border:1px solid #ddd;&quot;&gt;func 형태&lt;/th&gt;
        &lt;th style=&quot;padding:10px 16px; text-align:left; border:1px solid #ddd;&quot;&gt;예시&lt;/th&gt;
        &lt;th style=&quot;padding:10px 16px; text-align:left; border:1px solid #ddd;&quot;&gt;결과&lt;/th&gt;
      &lt;/tr&gt;
    &lt;/thead&gt;
    &lt;tbody&gt;
      &lt;tr style=&quot;background:#f9fafb;&quot;&gt;
        &lt;td style=&quot;padding:10px 16px; border:1px solid #ddd;&quot;&gt;문자열&lt;/td&gt;
        &lt;td style=&quot;padding:10px 16px; border:1px solid #ddd;&quot;&gt;&lt;code style=&quot;background:#eef2ff; color:#4f46e5; padding:2px 6px; border-radius:4px;&quot;&gt;agg('sum')&lt;/code&gt;&lt;/td&gt;
        &lt;td style=&quot;padding:10px 16px; border:1px solid #ddd;&quot;&gt;Series&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
        &lt;td style=&quot;padding:10px 16px; border:1px solid #ddd;&quot;&gt;리스트&lt;/td&gt;
        &lt;td style=&quot;padding:10px 16px; border:1px solid #ddd;&quot;&gt;&lt;code style=&quot;background:#eef2ff; color:#4f46e5; padding:2px 6px; border-radius:4px;&quot;&gt;agg(['sum', 'mean'])&lt;/code&gt;&lt;/td&gt;
        &lt;td style=&quot;padding:10px 16px; border:1px solid #ddd;&quot;&gt;DataFrame (함수명이 행)&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr style=&quot;background:#f9fafb;&quot;&gt;
        &lt;td style=&quot;padding:10px 16px; border:1px solid #ddd;&quot;&gt;딕셔너리&lt;/td&gt;
        &lt;td style=&quot;padding:10px 16px; border:1px solid #ddd;&quot;&gt;&lt;code style=&quot;background:#eef2ff; color:#4f46e5; padding:2px 6px; border-radius:4px;&quot;&gt;agg({'A': 'sum', 'B': 'mean'})&lt;/code&gt;&lt;/td&gt;
        &lt;td style=&quot;padding:10px 16px; border:1px solid #ddd;&quot;&gt;DataFrame (열별 다른 함수)&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
        &lt;td style=&quot;padding:10px 16px; border:1px solid #ddd;&quot;&gt;Named agg&lt;/td&gt;
        &lt;td style=&quot;padding:10px 16px; border:1px solid #ddd;&quot;&gt;&lt;code style=&quot;background:#eef2ff; color:#4f46e5; padding:2px 6px; border-radius:4px;&quot;&gt;agg(결과명=('열', '함수'))&lt;/code&gt;&lt;/td&gt;
        &lt;td style=&quot;padding:10px 16px; border:1px solid #ddd;&quot;&gt;깔끔한 DataFrame&lt;/td&gt;
      &lt;/tr&gt;
    &lt;/tbody&gt;
  &lt;/table&gt;

  &lt;br/&gt;

  &lt;h2 style=&quot;font-size: 22px; font-weight: 700; color: #1a1a1a; margin-top: 40px; margin-bottom: 16px;&quot;&gt;1. 단일 함수&lt;/h2&gt;

  &lt;pre style=&quot;background:#1e1e2e; color:#cdd6f4; padding:20px 24px; border-radius:8px; font-size:14px; line-height:1.7; overflow-x:auto; margin-bottom:24px;&quot;&gt;&lt;code&gt;&lt;span style=&quot;color:#89b4fa;&quot;&gt;import&lt;/span&gt; pandas &lt;span style=&quot;color:#89b4fa;&quot;&gt;as&lt;/span&gt; pd

df = pd.DataFrame({
    '매출': [100, 200, 150, 300],
    '수량': [10,  20,  15,  30]
})

df.agg('sum')
&lt;span style=&quot;color:#6c7086;&quot;&gt;# 매출    750&lt;/span&gt;
&lt;span style=&quot;color:#6c7086;&quot;&gt;# 수량     75&lt;/span&gt;
&lt;span style=&quot;color:#6c7086;&quot;&gt;# dtype: int64&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

  &lt;br/&gt;

  &lt;h2 style=&quot;font-size: 22px; font-weight: 700; color: #1a1a1a; margin-top: 40px; margin-bottom: 16px;&quot;&gt;2. 여러 함수를 리스트로&lt;/h2&gt;

  &lt;pre style=&quot;background:#1e1e2e; color:#cdd6f4; padding:20px 24px; border-radius:8px; font-size:14px; line-height:1.7; overflow-x:auto; margin-bottom:24px;&quot;&gt;&lt;code&gt;df.agg(['sum', 'mean', 'max'])
&lt;span style=&quot;color:#6c7086;&quot;&gt;#        매출     수량&lt;/span&gt;
&lt;span style=&quot;color:#6c7086;&quot;&gt;# sum   750.0   75.00&lt;/span&gt;
&lt;span style=&quot;color:#6c7086;&quot;&gt;# mean  187.5   18.75&lt;/span&gt;
&lt;span style=&quot;color:#6c7086;&quot;&gt;# max   300.0   30.00&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

  &lt;br/&gt;

  &lt;h2 style=&quot;font-size: 22px; font-weight: 700; color: #1a1a1a; margin-top: 40px; margin-bottom: 16px;&quot;&gt;3. 열마다 다른 함수를 딕셔너리로&lt;/h2&gt;

  &lt;pre style=&quot;background:#1e1e2e; color:#cdd6f4; padding:20px 24px; border-radius:8px; font-size:14px; line-height:1.7; overflow-x:auto; margin-bottom:24px;&quot;&gt;&lt;code&gt;df.agg({
    '매출': ['sum', 'mean'],
    '수량': 'max'
})
&lt;span style=&quot;color:#6c7086;&quot;&gt;#        매출    수량&lt;/span&gt;
&lt;span style=&quot;color:#6c7086;&quot;&gt;# sum   750.0   NaN&lt;/span&gt;
&lt;span style=&quot;color:#6c7086;&quot;&gt;# mean  187.5   NaN&lt;/span&gt;
&lt;span style=&quot;color:#6c7086;&quot;&gt;# max    NaN   30.0&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

  &lt;br/&gt;

  &lt;h2 style=&quot;font-size: 22px; font-weight: 700; color: #1a1a1a; margin-top: 40px; margin-bottom: 16px;&quot;&gt;4. &lt;code style=&quot;background:#eef2ff; color:#4f46e5; padding:2px 8px; border-radius:4px; font-size:85%;&quot;&gt;groupby&lt;/code&gt; + &lt;code style=&quot;background:#eef2ff; color:#4f46e5; padding:2px 8px; border-radius:4px; font-size:85%;&quot;&gt;agg&lt;/code&gt; — 가장 많이 쓰는 패턴&lt;/h2&gt;

  &lt;pre style=&quot;background:#1e1e2e; color:#cdd6f4; padding:20px 24px; border-radius:8px; font-size:14px; line-height:1.7; overflow-x:auto; margin-bottom:24px;&quot;&gt;&lt;code&gt;sales = pd.DataFrame({
    '지역':  ['서울', '서울', '부산', '부산', '서울'],
    '매출':  [100, 200, 150, 300, 120],
    '수량':  [10,  20,  15,  30,  12]
})

sales.groupby('지역').agg({
    '매출': ['sum', 'mean'],
    '수량': 'sum'
})
&lt;span style=&quot;color:#6c7086;&quot;&gt;#        매출              수량&lt;/span&gt;
&lt;span style=&quot;color:#6c7086;&quot;&gt;#         sum    mean      sum&lt;/span&gt;
&lt;span style=&quot;color:#6c7086;&quot;&gt;# 지역&lt;/span&gt;
&lt;span style=&quot;color:#6c7086;&quot;&gt;# 부산    450  225.0       45&lt;/span&gt;
&lt;span style=&quot;color:#6c7086;&quot;&gt;# 서울    420  140.0       42&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

  &lt;br/&gt;

  &lt;h2 style=&quot;font-size: 22px; font-weight: 700; color: #1a1a1a; margin-top: 40px; margin-bottom: 16px;&quot;&gt;5. Named Aggregation — 결과 컬럼명 직접 지정&lt;/h2&gt;

  &lt;p style=&quot;margin: 0 0 16px;&quot;&gt;멀티인덱스 컬럼 없이 깔끔한 결과를 얻을 수 있어 실무에서 가장 선호하는 패턴입니다.&lt;/p&gt;

  &lt;pre style=&quot;background:#1e1e2e; color:#cdd6f4; padding:20px 24px; border-radius:8px; font-size:14px; line-height:1.7; overflow-x:auto; margin-bottom:24px;&quot;&gt;&lt;code&gt;sales.groupby('지역').agg(
    매출합계=('매출', 'sum'),
    평균매출=('매출', 'mean'),
    총수량  =('수량', 'sum')
)
&lt;span style=&quot;color:#6c7086;&quot;&gt;#        매출합계  평균매출  총수량&lt;/span&gt;
&lt;span style=&quot;color:#6c7086;&quot;&gt;# 지역&lt;/span&gt;
&lt;span style=&quot;color:#6c7086;&quot;&gt;# 부산      450   225.0     45&lt;/span&gt;
&lt;span style=&quot;color:#6c7086;&quot;&gt;# 서울      420   140.0     42&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

  &lt;br/&gt;

  &lt;h2 style=&quot;font-size: 22px; font-weight: 700; color: #1a1a1a; margin-top: 40px; margin-bottom: 16px;&quot;&gt;6. 커스텀 함수 적용&lt;/h2&gt;

  &lt;pre style=&quot;background:#1e1e2e; color:#cdd6f4; padding:20px 24px; border-radius:8px; font-size:14px; line-height:1.7; overflow-x:auto; margin-bottom:24px;&quot;&gt;&lt;code&gt;&lt;span style=&quot;color:#89b4fa;&quot;&gt;def&lt;/span&gt; range_val(x):
    &lt;span style=&quot;color:#89b4fa;&quot;&gt;return&lt;/span&gt; x.max() - x.min()

sales.groupby('지역').agg(
    매출범위=('매출', range_val),
    매출합계=('매출', 'sum')
)
&lt;span style=&quot;color:#6c7086;&quot;&gt;#        매출범위  매출합계&lt;/span&gt;
&lt;span style=&quot;color:#6c7086;&quot;&gt;# 지역&lt;/span&gt;
&lt;span style=&quot;color:#6c7086;&quot;&gt;# 부산      150      450&lt;/span&gt;
&lt;span style=&quot;color:#6c7086;&quot;&gt;# 서울      100      420&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

  &lt;br/&gt;

  &lt;h2 style=&quot;font-size: 22px; font-weight: 700; color: #1a1a1a; margin-top: 40px; margin-bottom: 16px;&quot;&gt;응용 사례 1 — 데이터 품질 점검&lt;/h2&gt;

  &lt;pre style=&quot;background:#1e1e2e; color:#cdd6f4; padding:20px 24px; border-radius:8px; font-size:14px; line-height:1.7; overflow-x:auto; margin-bottom:24px;&quot;&gt;&lt;code&gt;df = pd.DataFrame({
    '나이':  [25, None, 30, 22, None],
    '점수':  [80, 90,  None, 70, 85]
})

df.agg(
    건수   =('나이', 'count'),
    결측수  =('나이', &lt;span style=&quot;color:#89b4fa;&quot;&gt;lambda&lt;/span&gt; x: x.isna().sum()),
    평균나이 =('나이', 'mean'),
    평균점수 =('점수', 'mean')
)
&lt;span style=&quot;color:#6c7086;&quot;&gt;#          나이    점수&lt;/span&gt;
&lt;span style=&quot;color:#6c7086;&quot;&gt;# 건수      3.0    4.0&lt;/span&gt;
&lt;span style=&quot;color:#6c7086;&quot;&gt;# 결측수    2.0    1.0&lt;/span&gt;
&lt;span style=&quot;color:#6c7086;&quot;&gt;# 평균나이  25.7   NaN&lt;/span&gt;
&lt;span style=&quot;color:#6c7086;&quot;&gt;# 평균점수   NaN  81.25&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

  &lt;br/&gt;

  &lt;h2 style=&quot;font-size: 22px; font-weight: 700; color: #1a1a1a; margin-top: 40px; margin-bottom: 16px;&quot;&gt;응용 사례 2 — 기간별 매출 리포트&lt;/h2&gt;

  &lt;pre style=&quot;background:#1e1e2e; color:#cdd6f4; padding:20px 24px; border-radius:8px; font-size:14px; line-height:1.7; overflow-x:auto; margin-bottom:24px;&quot;&gt;&lt;code&gt;report = sales_df.groupby('월').agg(
    총매출  =('매출', 'sum'),
    평균매출 =('매출', 'mean'),
    최대매출 =('매출', 'max'),
    거래건수 =('매출', 'count')
).round(1)
&lt;span style=&quot;color:#6c7086;&quot;&gt;#     총매출  평균매출  최대매출  거래건수&lt;/span&gt;
&lt;span style=&quot;color:#6c7086;&quot;&gt;# 월&lt;/span&gt;
&lt;span style=&quot;color:#6c7086;&quot;&gt;# 1   1200   240.0    500      5&lt;/span&gt;
&lt;span style=&quot;color:#6c7086;&quot;&gt;# 2   1450   290.0    620      5&lt;/span&gt;
&lt;span style=&quot;color:#6c7086;&quot;&gt;# 3   1380   276.0    480      5&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

  &lt;br/&gt;

  &lt;h2 style=&quot;font-size: 22px; font-weight: 700; color: #1a1a1a; margin-top: 40px; margin-bottom: 16px;&quot;&gt;응용 사례 3 — 다중 그룹 집계&lt;/h2&gt;

  &lt;pre style=&quot;background:#1e1e2e; color:#cdd6f4; padding:20px 24px; border-radius:8px; font-size:14px; line-height:1.7; overflow-x:auto; margin-bottom:24px;&quot;&gt;&lt;code&gt;result = sales.groupby(['지역', '카테고리']).agg(
    매출합계=('매출', 'sum'),
    평균단가=('단가', 'mean'),
    판매량  =('수량', 'sum')
)
&lt;span style=&quot;color:#6c7086;&quot;&gt;#               매출합계  평균단가  판매량&lt;/span&gt;
&lt;span style=&quot;color:#6c7086;&quot;&gt;# 지역  카테고리&lt;/span&gt;
&lt;span style=&quot;color:#6c7086;&quot;&gt;# 부산  식품        350     8.5     42&lt;/span&gt;
&lt;span style=&quot;color:#6c7086;&quot;&gt;#      전자        480    24.0     20&lt;/span&gt;
&lt;span style=&quot;color:#6c7086;&quot;&gt;# 서울  식품        290     7.2     40&lt;/span&gt;
&lt;span style=&quot;color:#6c7086;&quot;&gt;#      전자        620    31.0     20&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

  &lt;br/&gt;

  &lt;h2 style=&quot;font-size: 22px; font-weight: 700; color: #1a1a1a; margin-top: 40px; margin-bottom: 16px;&quot;&gt;자주 쓰는 집계 함수 목록&lt;/h2&gt;

  &lt;table style=&quot;width:100%; border-collapse:collapse; margin-bottom:24px; font-size:15px;&quot;&gt;
    &lt;thead&gt;
      &lt;tr style=&quot;background:#4f8ef7; color:#fff;&quot;&gt;
        &lt;th style=&quot;padding:10px 16px; text-align:left; border:1px solid #ddd;&quot;&gt;문자열&lt;/th&gt;
        &lt;th style=&quot;padding:10px 16px; text-align:left; border:1px solid #ddd;&quot;&gt;의미&lt;/th&gt;
      &lt;/tr&gt;
    &lt;/thead&gt;
    &lt;tbody&gt;
      &lt;tr style=&quot;background:#f9fafb;&quot;&gt;
        &lt;td style=&quot;padding:10px 16px; border:1px solid #ddd;&quot;&gt;&lt;code style=&quot;background:#eef2ff; color:#4f46e5; padding:2px 6px; border-radius:4px;&quot;&gt;'sum'&lt;/code&gt;&lt;/td&gt;
        &lt;td style=&quot;padding:10px 16px; border:1px solid #ddd;&quot;&gt;합계&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
        &lt;td style=&quot;padding:10px 16px; border:1px solid #ddd;&quot;&gt;&lt;code style=&quot;background:#eef2ff; color:#4f46e5; padding:2px 6px; border-radius:4px;&quot;&gt;'mean'&lt;/code&gt;&lt;/td&gt;
        &lt;td style=&quot;padding:10px 16px; border:1px solid #ddd;&quot;&gt;평균&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr style=&quot;background:#f9fafb;&quot;&gt;
        &lt;td style=&quot;padding:10px 16px; border:1px solid #ddd;&quot;&gt;&lt;code style=&quot;background:#eef2ff; color:#4f46e5; padding:2px 6px; border-radius:4px;&quot;&gt;'median'&lt;/code&gt;&lt;/td&gt;
        &lt;td style=&quot;padding:10px 16px; border:1px solid #ddd;&quot;&gt;중앙값&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
        &lt;td style=&quot;padding:10px 16px; border:1px solid #ddd;&quot;&gt;&lt;code style=&quot;background:#eef2ff; color:#4f46e5; padding:2px 6px; border-radius:4px;&quot;&gt;'min'&lt;/code&gt; / &lt;code style=&quot;background:#eef2ff; color:#4f46e5; padding:2px 6px; border-radius:4px;&quot;&gt;'max'&lt;/code&gt;&lt;/td&gt;
        &lt;td style=&quot;padding:10px 16px; border:1px solid #ddd;&quot;&gt;최솟값 / 최댓값&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr style=&quot;background:#f9fafb;&quot;&gt;
        &lt;td style=&quot;padding:10px 16px; border:1px solid #ddd;&quot;&gt;&lt;code style=&quot;background:#eef2ff; color:#4f46e5; padding:2px 6px; border-radius:4px;&quot;&gt;'count'&lt;/code&gt;&lt;/td&gt;
        &lt;td style=&quot;padding:10px 16px; border:1px solid #ddd;&quot;&gt;비결측 건수&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
        &lt;td style=&quot;padding:10px 16px; border:1px solid #ddd;&quot;&gt;&lt;code style=&quot;background:#eef2ff; color:#4f46e5; padding:2px 6px; border-radius:4px;&quot;&gt;'nunique'&lt;/code&gt;&lt;/td&gt;
        &lt;td style=&quot;padding:10px 16px; border:1px solid #ddd;&quot;&gt;고유값 수&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr style=&quot;background:#f9fafb;&quot;&gt;
        &lt;td style=&quot;padding:10px 16px; border:1px solid #ddd;&quot;&gt;&lt;code style=&quot;background:#eef2ff; color:#4f46e5; padding:2px 6px; border-radius:4px;&quot;&gt;'std'&lt;/code&gt; / &lt;code style=&quot;background:#eef2ff; color:#4f46e5; padding:2px 6px; border-radius:4px;&quot;&gt;'var'&lt;/code&gt;&lt;/td&gt;
        &lt;td style=&quot;padding:10px 16px; border:1px solid #ddd;&quot;&gt;표준편차 / 분산&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
        &lt;td style=&quot;padding:10px 16px; border:1px solid #ddd;&quot;&gt;&lt;code style=&quot;background:#eef2ff; color:#4f46e5; padding:2px 6px; border-radius:4px;&quot;&gt;'first'&lt;/code&gt; / &lt;code style=&quot;background:#eef2ff; color:#4f46e5; padding:2px 6px; border-radius:4px;&quot;&gt;'last'&lt;/code&gt;&lt;/td&gt;
        &lt;td style=&quot;padding:10px 16px; border:1px solid #ddd;&quot;&gt;첫 번째 / 마지막 값&lt;/td&gt;
      &lt;/tr&gt;
    &lt;/tbody&gt;
  &lt;/table&gt;

  &lt;div style=&quot;background:#fffbeb; border-left:4px solid #f59e0b; padding:14px 18px; border-radius:0 8px 8px 0; margin-bottom:24px; font-size:15px;&quot;&gt;
    &lt;strong&gt;핵심 직관:&lt;/strong&gt; &lt;code style=&quot;background:#eef2ff; color:#4f46e5; padding:2px 6px; border-radius:4px;&quot;&gt;agg&lt;/code&gt;는 &quot;여러 집계를 한 번에 정의하는 선언문&quot;입니다. &lt;code style=&quot;background:#eef2ff; color:#4f46e5; padding:2px 6px; border-radius:4px;&quot;&gt;groupby&lt;/code&gt; 이후 Named Aggregation 패턴(&lt;code style=&quot;background:#eef2ff; color:#4f46e5; padding:2px 6px; border-radius:4px;&quot;&gt;결과명=('열', '함수')&lt;/code&gt;)이 가독성과 편의성 모두 가장 좋습니다.
  &lt;/div&gt;

&lt;/div&gt;</description>
      <category>Python/Python Pandas</category>
      <category>agg</category>
      <category>PANDAS</category>
      <category>Python</category>
      <author>CosmosProject</author>
      <guid isPermaLink="true">https://cosmosproject.tistory.com/941</guid>
      <comments>https://cosmosproject.tistory.com/941#entry941comment</comments>
      <pubDate>Wed, 20 May 2026 21:50:34 +0900</pubDate>
    </item>
    <item>
      <title>Python Pandas : div (나눗셈)</title>
      <link>https://cosmosproject.tistory.com/940</link>
      <description>&lt;div style=&quot;font-family: 'Noto Sans KR', Apple SD Gothic Neo, sans-serif; font-size: 16px; line-height: 1.8; color: #333; max-width: 780px; margin: 0 auto;&quot;&gt;

  &lt;h1 style=&quot;font-size: 28px; font-weight: 700; color: #1a1a1a; border-bottom: 3px solid #4f8ef7; padding-bottom: 12px; margin-bottom: 32px;&quot;&gt;Pandas &lt;code style=&quot;background:#eef2ff; color:#4f46e5; padding:2px 8px; border-radius:4px; font-size:90%;&quot;&gt;div()&lt;/code&gt; — axis와 fill_value로 나눗셈 방향과 결측치를 제어하는 메서드&lt;/h1&gt;

  &lt;p style=&quot;margin: 0 0 24px;&quot;&gt;&lt;code style=&quot;background:#eef2ff; color:#4f46e5; padding:2px 8px; border-radius:4px; font-size:90%;&quot;&gt;div()&lt;/code&gt;는 DataFrame의 element-wise 나눗셈 메서드입니다. &lt;code style=&quot;background:#eef2ff; color:#4f46e5; padding:2px 8px; border-radius:4px; font-size:90%;&quot;&gt;/&lt;/code&gt; 연산자와 기본 동작은 같지만, &lt;code style=&quot;background:#eef2ff; color:#4f46e5; padding:2px 8px; border-radius:4px; font-size:90%;&quot;&gt;axis&lt;/code&gt;와 &lt;code style=&quot;background:#eef2ff; color:#4f46e5; padding:2px 8px; border-radius:4px; font-size:90%;&quot;&gt;fill_value&lt;/code&gt; 파라미터로 행/열 방향 제어와 결측치 처리가 가능합니다.&lt;/p&gt;

  &lt;h2 style=&quot;font-size: 22px; font-weight: 700; color: #1a1a1a; margin-top: 40px; margin-bottom: 16px;&quot;&gt;기본 문법&lt;/h2&gt;

  &lt;pre style=&quot;background:#1e1e2e; color:#cdd6f4; padding:20px 24px; border-radius:8px; font-size:14px; line-height:1.7; overflow-x:auto; margin-bottom:16px;&quot;&gt;&lt;code&gt;DataFrame.div(other, axis='columns', fill_value=None)&lt;/code&gt;&lt;/pre&gt;

  &lt;table style=&quot;width:100%; border-collapse:collapse; margin-bottom:24px; font-size:15px;&quot;&gt;
    &lt;thead&gt;
      &lt;tr style=&quot;background:#4f8ef7; color:#fff;&quot;&gt;
        &lt;th style=&quot;padding:10px 16px; text-align:left; border:1px solid #ddd;&quot;&gt;파라미터&lt;/th&gt;
        &lt;th style=&quot;padding:10px 16px; text-align:left; border:1px solid #ddd;&quot;&gt;설명&lt;/th&gt;
      &lt;/tr&gt;
    &lt;/thead&gt;
    &lt;tbody&gt;
      &lt;tr style=&quot;background:#f9fafb;&quot;&gt;
        &lt;td style=&quot;padding:10px 16px; border:1px solid #ddd;&quot;&gt;&lt;code style=&quot;background:#eef2ff; color:#4f46e5; padding:2px 6px; border-radius:4px;&quot;&gt;other&lt;/code&gt;&lt;/td&gt;
        &lt;td style=&quot;padding:10px 16px; border:1px solid #ddd;&quot;&gt;나눌 값 (스칼라, Series, DataFrame)&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
        &lt;td style=&quot;padding:10px 16px; border:1px solid #ddd;&quot;&gt;&lt;code style=&quot;background:#eef2ff; color:#4f46e5; padding:2px 6px; border-radius:4px;&quot;&gt;axis&lt;/code&gt;&lt;/td&gt;
        &lt;td style=&quot;padding:10px 16px; border:1px solid #ddd;&quot;&gt;&lt;code style=&quot;background:#eef2ff; color:#4f46e5; padding:2px 6px; border-radius:4px;&quot;&gt;0&lt;/code&gt;/'index' 또는 &lt;code style=&quot;background:#eef2ff; color:#4f46e5; padding:2px 6px; border-radius:4px;&quot;&gt;1&lt;/code&gt;/'columns' (기본값 1)&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr style=&quot;background:#f9fafb;&quot;&gt;
        &lt;td style=&quot;padding:10px 16px; border:1px solid #ddd;&quot;&gt;&lt;code style=&quot;background:#eef2ff; color:#4f46e5; padding:2px 6px; border-radius:4px;&quot;&gt;fill_value&lt;/code&gt;&lt;/td&gt;
        &lt;td style=&quot;padding:10px 16px; border:1px solid #ddd;&quot;&gt;연산 전 NaN을 대체할 값 (기본값 None)&lt;/td&gt;
      &lt;/tr&gt;
    &lt;/tbody&gt;
  &lt;/table&gt;

  &lt;br/&gt;

  &lt;h2 style=&quot;font-size: 22px; font-weight: 700; color: #1a1a1a; margin-top: 40px; margin-bottom: 16px;&quot;&gt;스칼라 나눗셈&lt;/h2&gt;

  &lt;pre style=&quot;background:#1e1e2e; color:#cdd6f4; padding:20px 24px; border-radius:8px; font-size:14px; line-height:1.7; overflow-x:auto; margin-bottom:24px;&quot;&gt;&lt;code&gt;&lt;span style=&quot;color:#89b4fa;&quot;&gt;import&lt;/span&gt; pandas &lt;span style=&quot;color:#89b4fa;&quot;&gt;as&lt;/span&gt; pd

df = pd.DataFrame({
    '국어': [80, 60, 90],
    '수학': [70, 85, 95]
})

df.div(100)
&lt;span style=&quot;color:#6c7086;&quot;&gt;#    국어  수학&lt;/span&gt;
&lt;span style=&quot;color:#6c7086;&quot;&gt;# 0  0.80  0.70&lt;/span&gt;
&lt;span style=&quot;color:#6c7086;&quot;&gt;# 1  0.60  0.85&lt;/span&gt;
&lt;span style=&quot;color:#6c7086;&quot;&gt;# 2  0.90  0.95&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

  &lt;br/&gt;

  &lt;h2 style=&quot;font-size: 22px; font-weight: 700; color: #1a1a1a; margin-top: 40px; margin-bottom: 16px;&quot;&gt;Series로 나눗셈 — &lt;code style=&quot;background:#eef2ff; color:#4f46e5; padding:2px 8px; border-radius:4px; font-size:85%;&quot;&gt;axis&lt;/code&gt;가 핵심&lt;/h2&gt;

  &lt;p style=&quot;margin: 0 0 16px;&quot;&gt;Series를 기준으로 나눌 때 &lt;strong&gt;axis 방향&lt;/strong&gt;을 반드시 지정해야 합니다.&lt;/p&gt;

  &lt;pre style=&quot;background:#1e1e2e; color:#cdd6f4; padding:20px 24px; border-radius:8px; font-size:14px; line-height:1.7; overflow-x:auto; margin-bottom:16px;&quot;&gt;&lt;code&gt;df:              total (행별 합계):
   국어  수학
0   80   70      0    150
1   60   85      1    145
2   90   95      2    185&lt;/code&gt;&lt;/pre&gt;

  &lt;pre style=&quot;background:#1e1e2e; color:#cdd6f4; padding:20px 24px; border-radius:8px; font-size:14px; line-height:1.7; overflow-x:auto; margin-bottom:24px;&quot;&gt;&lt;code&gt;total = df.sum(axis=1)   &lt;span style=&quot;color:#6c7086;&quot;&gt;# 행별 합계 → Series [150, 145, 185]&lt;/span&gt;

&lt;span style=&quot;color:#6c7086;&quot;&gt;# axis=0: Series 인덱스를 행에 맞춰 정렬 → 각 행을 해당 총점으로 나눔&lt;/span&gt;
df.div(total, axis=0)
&lt;span style=&quot;color:#6c7086;&quot;&gt;#        국어      수학&lt;/span&gt;
&lt;span style=&quot;color:#6c7086;&quot;&gt;# 0  0.5333    0.4667&lt;/span&gt;
&lt;span style=&quot;color:#6c7086;&quot;&gt;# 1  0.4138    0.5862&lt;/span&gt;
&lt;span style=&quot;color:#6c7086;&quot;&gt;# 2  0.4865    0.5135&lt;/span&gt;

&lt;span style=&quot;color:#6c7086;&quot;&gt;# axis=1 (기본): Series 인덱스를 열에 맞춰 정렬&lt;/span&gt;
&lt;span style=&quot;color:#6c7086;&quot;&gt;# → '국어', '수학' 라벨이 없으면 NaN 발생&lt;/span&gt;
df.div(total, axis=1)   &lt;span style=&quot;color:#6c7086;&quot;&gt;# 대부분 NaN&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

  &lt;div style=&quot;background:#fffbeb; border-left:4px solid #f59e0b; padding:14px 18px; border-radius:0 8px 8px 0; margin-bottom:24px; font-size:15px;&quot;&gt;
    &lt;strong&gt;axis 선택 기준:&lt;/strong&gt; 행별 합계로 나눌 때는 &lt;code style=&quot;background:#eef2ff; color:#4f46e5; padding:2px 6px; border-radius:4px;&quot;&gt;axis=0&lt;/code&gt;, 열별 합계로 나눌 때는 &lt;code style=&quot;background:#eef2ff; color:#4f46e5; padding:2px 6px; border-radius:4px;&quot;&gt;axis=1&lt;/code&gt;. 비율 계산은 거의 항상 &lt;code style=&quot;background:#eef2ff; color:#4f46e5; padding:2px 6px; border-radius:4px;&quot;&gt;axis=0&lt;/code&gt;입니다.
  &lt;/div&gt;

  &lt;br/&gt;

  &lt;h2 style=&quot;font-size: 22px; font-weight: 700; color: #1a1a1a; margin-top: 40px; margin-bottom: 16px;&quot;&gt;&lt;code style=&quot;background:#eef2ff; color:#4f46e5; padding:2px 8px; border-radius:4px; font-size:85%;&quot;&gt;fill_value&lt;/code&gt; — NaN 자리를 채우고 연산&lt;/h2&gt;

  &lt;p style=&quot;margin: 0 0 16px;&quot;&gt;DataFrame끼리 나눌 때 한쪽에만 NaN이 있으면 &lt;code style=&quot;background:#eef2ff; color:#4f46e5; padding:2px 8px; border-radius:4px; font-size:90%;&quot;&gt;fill_value&lt;/code&gt;로 채운 뒤 연산합니다. 양쪽 모두 NaN이면 여전히 NaN입니다.&lt;/p&gt;

  &lt;pre style=&quot;background:#1e1e2e; color:#cdd6f4; padding:20px 24px; border-radius:8px; font-size:14px; line-height:1.7; overflow-x:auto; margin-bottom:24px;&quot;&gt;&lt;code&gt;a = pd.DataFrame({'A': [10, None], 'B': [20, 30]})
b = pd.DataFrame({'A': [2,  4],    'B': [None, 6]})

a.div(b)
&lt;span style=&quot;color:#6c7086;&quot;&gt;#      A         B&lt;/span&gt;
&lt;span style=&quot;color:#6c7086;&quot;&gt;# 0  5.0       NaN   ← b의 B[0]이 NaN → 결과도 NaN&lt;/span&gt;
&lt;span style=&quot;color:#6c7086;&quot;&gt;# 1  NaN       5.0   ← a의 A[1]이 NaN → 결과도 NaN&lt;/span&gt;

a.div(b, fill_value=1)   &lt;span style=&quot;color:#6c7086;&quot;&gt;# NaN을 1로 채운 뒤 연산&lt;/span&gt;
&lt;span style=&quot;color:#6c7086;&quot;&gt;#      A         B&lt;/span&gt;
&lt;span style=&quot;color:#6c7086;&quot;&gt;# 0  5.0      20.0   ← b의 B[0]을 1로 채워서 20/1&lt;/span&gt;
&lt;span style=&quot;color:#6c7086;&quot;&gt;# 1  NaN       5.0   ← a의 A[1]은 NaN 유지 (둘 다 NaN이면 적용 안 됨)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

  &lt;br/&gt;

  &lt;h2 style=&quot;font-size: 22px; font-weight: 700; color: #1a1a1a; margin-top: 40px; margin-bottom: 16px;&quot;&gt;응용 사례 1 — 행별 비율 계산&lt;/h2&gt;

  &lt;pre style=&quot;background:#1e1e2e; color:#cdd6f4; padding:20px 24px; border-radius:8px; font-size:14px; line-height:1.7; overflow-x:auto; margin-bottom:24px;&quot;&gt;&lt;code&gt;scores = pd.DataFrame({
    '국어': [80, 60, 90],
    '수학': [70, 85, 95],
    '영어': [75, 80, 88]
}, index=['철수', '영희', '민준'])

total = scores.sum(axis=1)
ratio = scores.div(total, axis=0).round(3)
&lt;span style=&quot;color:#6c7086;&quot;&gt;#        국어    수학    영어&lt;/span&gt;
&lt;span style=&quot;color:#6c7086;&quot;&gt;# 철수  0.356  0.311  0.333&lt;/span&gt;
&lt;span style=&quot;color:#6c7086;&quot;&gt;# 영희  0.267  0.378  0.356&lt;/span&gt;
&lt;span style=&quot;color:#6c7086;&quot;&gt;# 민준  0.329  0.347  0.321&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

  &lt;br/&gt;

  &lt;h2 style=&quot;font-size: 22px; font-weight: 700; color: #1a1a1a; margin-top: 40px; margin-bottom: 16px;&quot;&gt;응용 사례 2 — 열별 최댓값으로 정규화&lt;/h2&gt;

  &lt;pre style=&quot;background:#1e1e2e; color:#cdd6f4; padding:20px 24px; border-radius:8px; font-size:14px; line-height:1.7; overflow-x:auto; margin-bottom:24px;&quot;&gt;&lt;code&gt;col_max = scores.max(axis=0)                   &lt;span style=&quot;color:#6c7086;&quot;&gt;# 열별 최댓값&lt;/span&gt;
normalized = scores.div(col_max, axis=1)       &lt;span style=&quot;color:#6c7086;&quot;&gt;# axis=1: 열 방향 정렬&lt;/span&gt;
&lt;span style=&quot;color:#6c7086;&quot;&gt;#         국어       수학       영어&lt;/span&gt;
&lt;span style=&quot;color:#6c7086;&quot;&gt;# 철수  0.8889    0.7368    0.8523&lt;/span&gt;
&lt;span style=&quot;color:#6c7086;&quot;&gt;# 영희  0.6667    0.8947    0.9091&lt;/span&gt;
&lt;span style=&quot;color:#6c7086;&quot;&gt;# 민준  1.0000    1.0000    1.0000&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

  &lt;br/&gt;

  &lt;h2 style=&quot;font-size: 22px; font-weight: 700; color: #1a1a1a; margin-top: 40px; margin-bottom: 16px;&quot;&gt;응용 사례 3 — 전월 대비 증감률&lt;/h2&gt;

  &lt;pre style=&quot;background:#1e1e2e; color:#cdd6f4; padding:20px 24px; border-radius:8px; font-size:14px; line-height:1.7; overflow-x:auto; margin-bottom:24px;&quot;&gt;&lt;code&gt;sales = pd.DataFrame({
    '서울': [100, 120, 110],
    '부산': [80,   75,  90]
}, index=['1월', '2월', '3월'])

&lt;span style=&quot;color:#6c7086;&quot;&gt;# 현재 / 이전 → 전월 대비 비율&lt;/span&gt;
growth = sales.div(sales.shift(1)).round(3)
&lt;span style=&quot;color:#6c7086;&quot;&gt;#        서울   부산&lt;/span&gt;
&lt;span style=&quot;color:#6c7086;&quot;&gt;# 1월     NaN    NaN   ← 이전 달 없음&lt;/span&gt;
&lt;span style=&quot;color:#6c7086;&quot;&gt;# 2월    1.20   0.938&lt;/span&gt;
&lt;span style=&quot;color:#6c7086;&quot;&gt;# 3월    0.917  1.200&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

  &lt;br/&gt;

  &lt;h2 style=&quot;font-size: 22px; font-weight: 700; color: #1a1a1a; margin-top: 40px; margin-bottom: 16px;&quot;&gt;응용 사례 4 — 카테고리별 매출 비중&lt;/h2&gt;

  &lt;pre style=&quot;background:#1e1e2e; color:#cdd6f4; padding:20px 24px; border-radius:8px; font-size:14px; line-height:1.7; overflow-x:auto; margin-bottom:24px;&quot;&gt;&lt;code&gt;data = pd.DataFrame({
    '식품': [200, 180, 220],
    '전자': [500, 620, 480]
}, index=['1월', '2월', '3월'])

row_total = data.sum(axis=1)
share = data.div(row_total, axis=0).mul(100).round(1)
&lt;span style=&quot;color:#6c7086;&quot;&gt;#     식품    전자&lt;/span&gt;
&lt;span style=&quot;color:#6c7086;&quot;&gt;# 1월  28.6   71.4&lt;/span&gt;
&lt;span style=&quot;color:#6c7086;&quot;&gt;# 2월  22.5   77.5&lt;/span&gt;
&lt;span style=&quot;color:#6c7086;&quot;&gt;# 3월  31.4   68.6&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

  &lt;br/&gt;

  &lt;h2 style=&quot;font-size: 22px; font-weight: 700; color: #1a1a1a; margin-top: 40px; margin-bottom: 16px;&quot;&gt;&lt;code style=&quot;background:#eef2ff; color:#4f46e5; padding:2px 8px; border-radius:4px; font-size:85%;&quot;&gt;div()&lt;/code&gt; vs &lt;code style=&quot;background:#eef2ff; color:#4f46e5; padding:2px 8px; border-radius:4px; font-size:85%;&quot;&gt;/&lt;/code&gt; 연산자&lt;/h2&gt;

  &lt;table style=&quot;width:100%; border-collapse:collapse; margin-bottom:24px; font-size:15px;&quot;&gt;
    &lt;thead&gt;
      &lt;tr style=&quot;background:#4f8ef7; color:#fff;&quot;&gt;
        &lt;th style=&quot;padding:10px 16px; text-align:left; border:1px solid #ddd;&quot;&gt;&lt;/th&gt;
        &lt;th style=&quot;padding:10px 16px; text-align:left; border:1px solid #ddd;&quot;&gt;&lt;code style=&quot;background:rgba(255,255,255,0.2); padding:2px 6px; border-radius:4px; font-size:90%;&quot;&gt;df / other&lt;/code&gt;&lt;/th&gt;
        &lt;th style=&quot;padding:10px 16px; text-align:left; border:1px solid #ddd;&quot;&gt;&lt;code style=&quot;background:rgba(255,255,255,0.2); padding:2px 6px; border-radius:4px; font-size:90%;&quot;&gt;df.div(other)&lt;/code&gt;&lt;/th&gt;
      &lt;/tr&gt;
    &lt;/thead&gt;
    &lt;tbody&gt;
      &lt;tr style=&quot;background:#f9fafb;&quot;&gt;
        &lt;td style=&quot;padding:10px 16px; border:1px solid #ddd;&quot;&gt;axis 지정&lt;/td&gt;
        &lt;td style=&quot;padding:10px 16px; border:1px solid #ddd;&quot;&gt;불가&lt;/td&gt;
        &lt;td style=&quot;padding:10px 16px; border:1px solid #ddd;&quot;&gt;가능&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
        &lt;td style=&quot;padding:10px 16px; border:1px solid #ddd;&quot;&gt;fill_value 지정&lt;/td&gt;
        &lt;td style=&quot;padding:10px 16px; border:1px solid #ddd;&quot;&gt;불가&lt;/td&gt;
        &lt;td style=&quot;padding:10px 16px; border:1px solid #ddd;&quot;&gt;가능&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr style=&quot;background:#f9fafb;&quot;&gt;
        &lt;td style=&quot;padding:10px 16px; border:1px solid #ddd;&quot;&gt;Series로 행별 나눗셈&lt;/td&gt;
        &lt;td style=&quot;padding:10px 16px; border:1px solid #ddd;&quot;&gt;불가 (NaN 발생)&lt;/td&gt;
        &lt;td style=&quot;padding:10px 16px; border:1px solid #ddd;&quot;&gt;&lt;code style=&quot;background:#eef2ff; color:#4f46e5; padding:2px 6px; border-radius:4px;&quot;&gt;axis=0&lt;/code&gt;으로 가능&lt;/td&gt;
      &lt;/tr&gt;
    &lt;/tbody&gt;
  &lt;/table&gt;

  &lt;div style=&quot;background:#fffbeb; border-left:4px solid #f59e0b; padding:14px 18px; border-radius:0 8px 8px 0; margin-bottom:24px; font-size:15px;&quot;&gt;
    &lt;strong&gt;핵심 직관:&lt;/strong&gt; &lt;code style=&quot;background:#eef2ff; color:#4f46e5; padding:2px 6px; border-radius:4px;&quot;&gt;axis=0&lt;/code&gt;은 &quot;각 행마다 Series 값 하나씩 대응&quot;, &lt;code style=&quot;background:#eef2ff; color:#4f46e5; padding:2px 6px; border-radius:4px;&quot;&gt;axis=1&lt;/code&gt;은 &quot;각 열마다 Series 값 하나씩 대응&quot;입니다. &lt;code style=&quot;background:#eef2ff; color:#4f46e5; padding:2px 6px; border-radius:4px;&quot;&gt;/&lt;/code&gt; 대신 &lt;code style=&quot;background:#eef2ff; color:#4f46e5; padding:2px 6px; border-radius:4px;&quot;&gt;div()&lt;/code&gt;를 써야 하는 순간은 행 방향(axis=0) 연산이 필요할 때입니다.
  &lt;/div&gt;

&lt;/div&gt;</description>
      <category>Python/Python Pandas</category>
      <category>div</category>
      <category>PANDAS</category>
      <category>Python</category>
      <author>CosmosProject</author>
      <guid isPermaLink="true">https://cosmosproject.tistory.com/940</guid>
      <comments>https://cosmosproject.tistory.com/940#entry940comment</comments>
      <pubDate>Wed, 20 May 2026 21:47:40 +0900</pubDate>
    </item>
    <item>
      <title>Python Pandas : stack, unstack (pivot하기, pivot 풀기)</title>
      <link>https://cosmosproject.tistory.com/939</link>
      <description>&lt;div style=&quot;font-family: 'Noto Sans KR', Apple SD Gothic Neo, sans-serif; font-size: 16px; line-height: 1.8; color: #333; max-width: 780px; margin: 0 auto;&quot;&gt;
&lt;h1 style=&quot;font-size: 28px; font-weight: bold; color: #1a1a1a; border-bottom: 3px solid #4f8ef7; padding-bottom: 12px; margin-bottom: 32px;&quot;&gt;Pandas &lt;code style=&quot;background: #eef2ff; color: #4f46e5; padding: 2px 8px; border-radius: 4px; font-size: 90%;&quot;&gt;stack()&lt;/code&gt; / &lt;code style=&quot;background: #eef2ff; color: #4f46e5; padding: 2px 8px; border-radius: 4px; font-size: 90%;&quot;&gt;unstack()&lt;/code&gt; &amp;mdash; 데이터를 넓은 형태와 긴 형태로 변환하는 메서드&lt;/h1&gt;
&lt;p style=&quot;margin: 0 0 24px;&quot; data-ke-size=&quot;size16&quot;&gt;열을 행으로 접거나(stack), 행 인덱스를 열로 펼치는(unstack) 메서드입니다. MultiIndex 데이터를 다룰 때 핵심이 되는 한 쌍의 역함수입니다.&lt;/p&gt;
&lt;h2 style=&quot;font-size: 22px; font-weight: bold; color: #1a1a1a; margin-top: 40px; margin-bottom: 16px;&quot; data-ke-size=&quot;size26&quot;&gt;핵심 개념 &amp;mdash; 넓은 형태 vs 긴 형태&lt;/h2&gt;
&lt;p style=&quot;margin: 0 0 16px;&quot; data-ke-size=&quot;size16&quot;&gt;데이터는 &lt;b&gt;넓은 형태(wide)&lt;/b&gt;와 &lt;b&gt;긴 형태(long)&lt;/b&gt; 두 가지로 표현할 수 있습니다.&lt;/p&gt;
&lt;pre class=&quot;angelscript&quot; style=&quot;background: #1e1e2e; color: #cdd6f4; padding: 20px 24px; border-radius: 8px; font-size: 14px; line-height: 1.7; overflow-x: auto; margin-bottom: 24px;&quot;&gt;&lt;code&gt;넓은 형태 (wide)          긴 형태 (long)
─────────────────         ──────────────────────
분기   Q1   Q2            지역  분기  매출
지역                      부산  Q1    80
부산   80  120     &amp;larr;&amp;rarr;     부산  Q2   120
서울  100  150            서울  Q1   100
                          서울  Q2   150

      unstack() &amp;larr;
      &amp;rarr; stack()&lt;/code&gt;&lt;/pre&gt;
&lt;table style=&quot;width: 100%; border-collapse: collapse; margin-bottom: 24px; font-size: 15px;&quot; data-ke-align=&quot;alignLeft&quot;&gt;
&lt;thead&gt;
&lt;tr style=&quot;background: #4f8ef7; color: #fff;&quot;&gt;
&lt;th style=&quot;padding: 10px 16px; text-align: left; border: 1px solid #ddd;&quot;&gt;&amp;nbsp;&lt;/th&gt;
&lt;th style=&quot;padding: 10px 16px; text-align: left; border: 1px solid #ddd;&quot;&gt;&lt;code style=&quot;background: rgba(255,255,255,0.2); padding: 2px 6px; border-radius: 4px; font-size: 90%;&quot;&gt;stack()&lt;/code&gt;&lt;/th&gt;
&lt;th style=&quot;padding: 10px 16px; text-align: left; border: 1px solid #ddd;&quot;&gt;&lt;code style=&quot;background: rgba(255,255,255,0.2); padding: 2px 6px; border-radius: 4px; font-size: 90%;&quot;&gt;unstack()&lt;/code&gt;&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr style=&quot;background: #f9fafb;&quot;&gt;
&lt;td style=&quot;padding: 10px 16px; border: 1px solid #ddd;&quot;&gt;방향&lt;/td&gt;
&lt;td style=&quot;padding: 10px 16px; border: 1px solid #ddd;&quot;&gt;열 &amp;rarr; 행 인덱스&lt;/td&gt;
&lt;td style=&quot;padding: 10px 16px; border: 1px solid #ddd;&quot;&gt;행 인덱스 &amp;rarr; 열&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td style=&quot;padding: 10px 16px; border: 1px solid #ddd;&quot;&gt;결과 형태&lt;/td&gt;
&lt;td style=&quot;padding: 10px 16px; border: 1px solid #ddd;&quot;&gt;긴 형태 (long)&lt;/td&gt;
&lt;td style=&quot;padding: 10px 16px; border: 1px solid #ddd;&quot;&gt;넓은 형태 (wide)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr style=&quot;background: #f9fafb;&quot;&gt;
&lt;td style=&quot;padding: 10px 16px; border: 1px solid #ddd;&quot;&gt;반환 타입&lt;/td&gt;
&lt;td style=&quot;padding: 10px 16px; border: 1px solid #ddd;&quot;&gt;MultiIndex Series&lt;/td&gt;
&lt;td style=&quot;padding: 10px 16px; border: 1px solid #ddd;&quot;&gt;DataFrame&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td style=&quot;padding: 10px 16px; border: 1px solid #ddd;&quot;&gt;주 사용처&lt;/td&gt;
&lt;td style=&quot;padding: 10px 16px; border: 1px solid #ddd;&quot;&gt;분석/집계용 변환&lt;/td&gt;
&lt;td style=&quot;padding: 10px 16px; border: 1px solid #ddd;&quot;&gt;피벗 테이블, 시각화 준비&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;br /&gt;
&lt;h2 style=&quot;font-size: 22px; font-weight: bold; color: #1a1a1a; margin-top: 40px; margin-bottom: 16px;&quot; data-ke-size=&quot;size26&quot;&gt;기본 문법&lt;/h2&gt;
&lt;pre class=&quot;routeros&quot; style=&quot;background: #1e1e2e; color: #cdd6f4; padding: 20px 24px; border-radius: 8px; font-size: 14px; line-height: 1.7; overflow-x: auto; margin-bottom: 24px;&quot;&gt;&lt;code&gt;DataFrame.stack(level=-1)                        # 가장 안쪽 열 레벨을 행으로
DataFrame.unstack(level=-1, fill_value=None)     # 가장 안쪽 행 레벨을 열로&lt;/code&gt;&lt;/pre&gt;
&lt;br /&gt;
&lt;h2 style=&quot;font-size: 22px; font-weight: bold; color: #1a1a1a; margin-top: 40px; margin-bottom: 16px;&quot; data-ke-size=&quot;size26&quot;&gt;&lt;code style=&quot;background: #eef2ff; color: #4f46e5; padding: 2px 8px; border-radius: 4px; font-size: 85%;&quot;&gt;stack()&lt;/code&gt; &amp;mdash; 열을 행으로 접기&lt;/h2&gt;
&lt;pre class=&quot;vala&quot; style=&quot;background: #1e1e2e; color: #cdd6f4; padding: 20px 24px; border-radius: 8px; font-size: 14px; line-height: 1.7; overflow-x: auto; margin-bottom: 16px;&quot;&gt;&lt;code&gt;import pandas as pd

wide = pd.DataFrame({
    'Q1': [80, 100],
    'Q2': [120, 150]
}, index=['부산', '서울'])

print(wide)
#       Q1   Q2
# 부산   80  120
# 서울  100  150

long = wide.stack()
print(long)
# 부산  Q1     80
#      Q2    120
# 서울  Q1    100
#      Q2    150
# dtype: int64&lt;/code&gt;&lt;/pre&gt;
&lt;p style=&quot;margin: 0 0 24px;&quot; data-ke-size=&quot;size16&quot;&gt;열(&lt;code style=&quot;background: #eef2ff; color: #4f46e5; padding: 2px 6px; border-radius: 4px;&quot;&gt;Q1&lt;/code&gt;, &lt;code style=&quot;background: #eef2ff; color: #4f46e5; padding: 2px 6px; border-radius: 4px;&quot;&gt;Q2&lt;/code&gt;)이 행 인덱스의 안쪽 레벨로 내려와 MultiIndex Series가 됩니다.&lt;/p&gt;
&lt;br /&gt;
&lt;h2 style=&quot;font-size: 22px; font-weight: bold; color: #1a1a1a; margin-top: 40px; margin-bottom: 16px;&quot; data-ke-size=&quot;size26&quot;&gt;&lt;code style=&quot;background: #eef2ff; color: #4f46e5; padding: 2px 8px; border-radius: 4px; font-size: 85%;&quot;&gt;unstack()&lt;/code&gt; &amp;mdash; 행을 열로 펼치기&lt;/h2&gt;
&lt;p style=&quot;margin: 0 0 16px;&quot; data-ke-size=&quot;size16&quot;&gt;&lt;code style=&quot;background: #eef2ff; color: #4f46e5; padding: 2px 8px; border-radius: 4px; font-size: 90%;&quot;&gt;stack()&lt;/code&gt;의 결과를 다시 원래대로 되돌립니다.&lt;/p&gt;
&lt;pre class=&quot;vala&quot; style=&quot;background: #1e1e2e; color: #cdd6f4; padding: 20px 24px; border-radius: 8px; font-size: 14px; line-height: 1.7; overflow-x: auto; margin-bottom: 24px;&quot;&gt;&lt;code&gt;wide_again = long.unstack()
print(wide_again)
#       Q1   Q2
# 부산   80  120
# 서울  100  150&lt;/code&gt;&lt;/pre&gt;
&lt;br /&gt;
&lt;h2 style=&quot;font-size: 22px; font-weight: bold; color: #1a1a1a; margin-top: 40px; margin-bottom: 16px;&quot; data-ke-size=&quot;size26&quot;&gt;&lt;code style=&quot;background: #eef2ff; color: #4f46e5; padding: 2px 8px; border-radius: 4px; font-size: 85%;&quot;&gt;level&lt;/code&gt; 파라미터 &amp;mdash; 어떤 레벨을 올릴지 선택&lt;/h2&gt;
&lt;p style=&quot;margin: 0 0 16px;&quot; data-ke-size=&quot;size16&quot;&gt;MultiIndex가 여러 레벨일 때 어떤 레벨을 열로 올릴지 지정합니다. &lt;code style=&quot;background: #eef2ff; color: #4f46e5; padding: 2px 8px; border-radius: 4px; font-size: 90%;&quot;&gt;groupby&lt;/code&gt;로 집계한 결과가 대표적인 예입니다.&lt;/p&gt;
&lt;pre class=&quot;vala&quot; style=&quot;background: #1e1e2e; color: #cdd6f4; padding: 20px 24px; border-radius: 8px; font-size: 14px; line-height: 1.7; overflow-x: auto; margin-bottom: 24px;&quot;&gt;&lt;code&gt;# groupby로 만든 MultiIndex Series
grouped = df.groupby(['지역', '분기'])['매출'].sum()
# 지역  분기
# 부산  Q1     80
#      Q2    120
# 서울  Q1    100
#      Q2    150

grouped.unstack()          # level=-1 (기본): 분기 &amp;rarr; 열
# 분기   Q1   Q2
# 지역
# 부산   80  120
# 서울  100  150

grouped.unstack(level=0)   # level=0: 지역 &amp;rarr; 열
# 지역  부산  서울
# 분기
# Q1     80   100
# Q2    120   150&lt;/code&gt;&lt;/pre&gt;
&lt;br /&gt;
&lt;h2 style=&quot;font-size: 22px; font-weight: bold; color: #1a1a1a; margin-top: 40px; margin-bottom: 16px;&quot; data-ke-size=&quot;size26&quot;&gt;&lt;code style=&quot;background: #eef2ff; color: #4f46e5; padding: 2px 8px; border-radius: 4px; font-size: 85%;&quot;&gt;fill_value&lt;/code&gt; &amp;mdash; 빈 자리 채우기&lt;/h2&gt;
&lt;p style=&quot;margin: 0 0 16px;&quot; data-ke-size=&quot;size16&quot;&gt;데이터에 없는 조합이 생기면 기본적으로 &lt;code style=&quot;background: #eef2ff; color: #4f46e5; padding: 2px 8px; border-radius: 4px; font-size: 90%;&quot;&gt;NaN&lt;/code&gt;이 채워집니다.&lt;/p&gt;
&lt;pre class=&quot;vala&quot; style=&quot;background: #1e1e2e; color: #cdd6f4; padding: 20px 24px; border-radius: 8px; font-size: 14px; line-height: 1.7; overflow-x: auto; margin-bottom: 24px;&quot;&gt;&lt;code&gt;# 부산 Q2 데이터가 없는 경우
grouped.unstack()
# 분기      Q1     Q2
# 지역
# 부산     80.0   NaN   &amp;larr; 빈 자리
# 서울    100.0  150.0

grouped.unstack(fill_value=0)
# 분기   Q1   Q2
# 지역
# 부산   80    0
# 서울  100  150&lt;/code&gt;&lt;/pre&gt;
&lt;br /&gt;
&lt;h2 style=&quot;font-size: 22px; font-weight: bold; color: #1a1a1a; margin-top: 40px; margin-bottom: 16px;&quot; data-ke-size=&quot;size26&quot;&gt;응용 사례 1 &amp;mdash; 월별 카테고리 매출 피벗&lt;/h2&gt;
&lt;pre class=&quot;vala&quot; style=&quot;background: #1e1e2e; color: #cdd6f4; padding: 20px 24px; border-radius: 8px; font-size: 14px; line-height: 1.7; overflow-x: auto; margin-bottom: 16px;&quot;&gt;&lt;code&gt;sales = pd.DataFrame({
    '월':      [1, 1, 2, 2, 3, 3],
    '카테고리': ['식품', '전자', '식품', '전자', '식품', '전자'],
    '매출':    [200, 500, 180, 620, 220, 480]
})

# groupby 집계 &amp;rarr; unstack으로 피벗
pivot = sales.groupby(['월', '카테고리'])['매출'].sum().unstack(fill_value=0)
# 카테고리  식품  전자
# 월
# 1        200  500
# 2        180  620
# 3        220  480

# 다시 긴 형태로 되돌리고 싶을 때
pivot.stack().reset_index()
#    월 카테고리  매출
# 0   1     식품   200
# 1   1     전자   500
# ...&lt;/code&gt;&lt;/pre&gt;
&lt;p style=&quot;margin: 0 0 24px;&quot; data-ke-size=&quot;size16&quot;&gt;피벗 형태는 &lt;code style=&quot;background: #eef2ff; color: #4f46e5; padding: 2px 6px; border-radius: 4px;&quot;&gt;.plot()&lt;/code&gt;으로 바로 시각화가 가능합니다.&lt;/p&gt;
&lt;br /&gt;
&lt;h2 style=&quot;font-size: 22px; font-weight: bold; color: #1a1a1a; margin-top: 40px; margin-bottom: 16px;&quot; data-ke-size=&quot;size26&quot;&gt;응용 사례 2 &amp;mdash; 교차 분석 및 비율 계산&lt;/h2&gt;
&lt;pre class=&quot;vala&quot; style=&quot;background: #1e1e2e; color: #cdd6f4; padding: 20px 24px; border-radius: 8px; font-size: 14px; line-height: 1.7; overflow-x: auto; margin-bottom: 24px;&quot;&gt;&lt;code&gt;survey = pd.DataFrame({
    '성별':   ['남', '남', '여', '여', '남', '여'],
    '만족도': ['만족', '불만', '만족', '만족', '만족', '불만'],
})

# 성별 &amp;times; 만족도 카운트
count = survey.groupby(['성별', '만족도']).size().unstack(fill_value=0)
# 만족도  만족  불만
# 성별
# 남       2    1
# 여       2    1

# 행별 비율 계산
ratio = count.div(count.sum(axis=1), axis=0).round(2)
# 만족도  만족  불만
# 성별
# 남      0.67  0.33
# 여      0.67  0.33&lt;/code&gt;&lt;/pre&gt;
&lt;br /&gt;
&lt;h2 style=&quot;font-size: 22px; font-weight: bold; color: #1a1a1a; margin-top: 40px; margin-bottom: 16px;&quot; data-ke-size=&quot;size26&quot;&gt;&lt;code style=&quot;background: #eef2ff; color: #4f46e5; padding: 2px 8px; border-radius: 4px; font-size: 85%;&quot;&gt;stack()&lt;/code&gt; / &lt;code style=&quot;background: #eef2ff; color: #4f46e5; padding: 2px 8px; border-radius: 4px; font-size: 85%;&quot;&gt;unstack()&lt;/code&gt; 역함수 관계&lt;/h2&gt;
&lt;pre class=&quot;pgsql&quot; style=&quot;background: #1e1e2e; color: #cdd6f4; padding: 20px 24px; border-radius: 8px; font-size: 14px; line-height: 1.7; overflow-x: auto; margin-bottom: 24px;&quot;&gt;&lt;code&gt;original  = pivot               # 넓은 형태
stacked   = pivot.stack()       # 긴 형태로 접기
restored  = stacked.unstack()   # 다시 넓은 형태로 복원

# original == restored  (완전히 동일)&lt;/code&gt;&lt;/pre&gt;
&lt;div style=&quot;background: #fffbeb; border-left: 4px solid #f59e0b; padding: 14px 18px; border-radius: 0 8px 8px 0; margin-bottom: 24px; font-size: 15px;&quot;&gt;&lt;b&gt;핵심 직관:&lt;/b&gt; &lt;code style=&quot;background: #eef2ff; color: #4f46e5; padding: 2px 6px; border-radius: 4px;&quot;&gt;stack&lt;/code&gt;은 표를 &lt;b&gt;세로로 접고&lt;/b&gt;, &lt;code style=&quot;background: #eef2ff; color: #4f46e5; padding: 2px 6px; border-radius: 4px;&quot;&gt;unstack&lt;/code&gt;은 &lt;b&gt;가로로 펼칩니다.&lt;/b&gt; 둘은 완전한 역함수 관계라 &lt;code style=&quot;background: #eef2ff; color: #4f46e5; padding: 2px 6px; border-radius: 4px;&quot;&gt;df.stack().unstack()&lt;/code&gt;은 원본과 동일합니다.&lt;/div&gt;
&lt;/div&gt;</description>
      <category>Python/Python numpy</category>
      <category>numpy</category>
      <category>Python</category>
      <category>Stack</category>
      <category>unstack</category>
      <author>CosmosProject</author>
      <guid isPermaLink="true">https://cosmosproject.tistory.com/939</guid>
      <comments>https://cosmosproject.tistory.com/939#entry939comment</comments>
      <pubDate>Wed, 20 May 2026 21:39:21 +0900</pubDate>
    </item>
    <item>
      <title>Python numpy : sum, mean</title>
      <link>https://cosmosproject.tistory.com/938</link>
      <description>&lt;div style=&quot;font-family: 'Noto Sans KR', Apple SD Gothic Neo, sans-serif; font-size: 16px; line-height: 1.8; color: #333; max-width: 780px; margin: 0 auto;&quot;&gt;

  &lt;h1 style=&quot;font-size: 28px; font-weight: 700; color: #1a1a1a; border-bottom: 3px solid #4f8ef7; padding-bottom: 12px; margin-bottom: 32px;&quot;&gt;NumPy &lt;code style=&quot;background:#eef2ff; color:#4f46e5; padding:2px 8px; border-radius:4px; font-size:90%;&quot;&gt;sum()&lt;/code&gt; / &lt;code style=&quot;background:#eef2ff; color:#4f46e5; padding:2px 8px; border-radius:4px; font-size:90%;&quot;&gt;mean()&lt;/code&gt; — 합계와 평균을 axis 방향으로 계산하는 메서드&lt;/h1&gt;

  &lt;p style=&quot;margin: 0 0 24px;&quot;&gt;배열 전체 또는 특정 축(axis) 방향으로 합계와 평균을 구합니다. axis 개념만 이해하면 행/열 단위 집계가 자유로워집니다.&lt;/p&gt;

  &lt;h2 style=&quot;font-size: 22px; font-weight: 700; color: #1a1a1a; margin-top: 40px; margin-bottom: 16px;&quot;&gt;axis 개념 이해&lt;/h2&gt;

  &lt;p style=&quot;margin: 0 0 16px;&quot;&gt;NumPy 연산의 핵심은 &lt;strong&gt;axis&lt;/strong&gt;입니다. &lt;code style=&quot;background:#eef2ff; color:#4f46e5; padding:2px 8px; border-radius:4px; font-size:90%;&quot;&gt;axis=0&lt;/code&gt;은 행을 따라 &lt;strong&gt;아래 방향(↓)&lt;/strong&gt;으로, &lt;code style=&quot;background:#eef2ff; color:#4f46e5; padding:2px 8px; border-radius:4px; font-size:90%;&quot;&gt;axis=1&lt;/code&gt;은 열을 따라 &lt;strong&gt;가로 방향(→)&lt;/strong&gt;으로 연산합니다.&lt;/p&gt;

  &lt;pre style=&quot;background:#1e1e2e; color:#cdd6f4; padding:20px 24px; border-radius:8px; font-size:14px; line-height:1.7; overflow-x:auto; margin-bottom:24px;&quot;&gt;&lt;code&gt;2D array:
        col0  col1  col2
row0  [[ 1,    2,    3 ],
row1   [ 4,    5,    6 ],
row2   [ 7,    8,    9 ]]

axis=0 → 행 방향(↓): 각 열을 따라 내려가며 연산 → 결과는 열마다 1개
axis=1 → 열 방향(→): 각 행을 따라 가로로 연산  → 결과는 행마다 1개&lt;/code&gt;&lt;/pre&gt;

  &lt;br/&gt;

  &lt;h2 style=&quot;font-size: 22px; font-weight: 700; color: #1a1a1a; margin-top: 40px; margin-bottom: 16px;&quot;&gt;기본 문법&lt;/h2&gt;

  &lt;pre style=&quot;background:#1e1e2e; color:#cdd6f4; padding:20px 24px; border-radius:8px; font-size:14px; line-height:1.7; overflow-x:auto; margin-bottom:24px;&quot;&gt;&lt;code&gt;numpy.sum(a, axis=None, keepdims=False)
numpy.mean(a, axis=None, keepdims=False)&lt;/code&gt;&lt;/pre&gt;

  &lt;br/&gt;

  &lt;h2 style=&quot;font-size: 22px; font-weight: 700; color: #1a1a1a; margin-top: 40px; margin-bottom: 16px;&quot;&gt;&lt;code style=&quot;background:#eef2ff; color:#4f46e5; padding:2px 8px; border-radius:4px; font-size:85%;&quot;&gt;np.sum()&lt;/code&gt; 작동 방식&lt;/h2&gt;

  &lt;pre style=&quot;background:#1e1e2e; color:#cdd6f4; padding:20px 24px; border-radius:8px; font-size:14px; line-height:1.7; overflow-x:auto; margin-bottom:24px;&quot;&gt;&lt;code&gt;&lt;span style=&quot;color:#89b4fa;&quot;&gt;import&lt;/span&gt; numpy &lt;span style=&quot;color:#89b4fa;&quot;&gt;as&lt;/span&gt; np

a = np.array([[1, 2, 3],
              [4, 5, 6],
              [7, 8, 9]])

np.sum(a)           &lt;span style=&quot;color:#6c7086;&quot;&gt;# 45  ← 전체 합계&lt;/span&gt;
np.sum(a, axis=0)   &lt;span style=&quot;color:#6c7086;&quot;&gt;# array([12, 15, 18])  ← 열별 합계 (1+4+7, 2+5+8, 3+6+9)&lt;/span&gt;
np.sum(a, axis=1)   &lt;span style=&quot;color:#6c7086;&quot;&gt;# array([ 6, 15, 24])  ← 행별 합계 (1+2+3, 4+5+6, 7+8+9)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

  &lt;br/&gt;

  &lt;h2 style=&quot;font-size: 22px; font-weight: 700; color: #1a1a1a; margin-top: 40px; margin-bottom: 16px;&quot;&gt;&lt;code style=&quot;background:#eef2ff; color:#4f46e5; padding:2px 8px; border-radius:4px; font-size:85%;&quot;&gt;np.mean()&lt;/code&gt; 작동 방식&lt;/h2&gt;

  &lt;pre style=&quot;background:#1e1e2e; color:#cdd6f4; padding:20px 24px; border-radius:8px; font-size:14px; line-height:1.7; overflow-x:auto; margin-bottom:24px;&quot;&gt;&lt;code&gt;np.mean(a)          &lt;span style=&quot;color:#6c7086;&quot;&gt;# 5.0  ← 전체 평균&lt;/span&gt;
np.mean(a, axis=0)  &lt;span style=&quot;color:#6c7086;&quot;&gt;# array([4., 5., 6.])  ← 열별 평균&lt;/span&gt;
np.mean(a, axis=1)  &lt;span style=&quot;color:#6c7086;&quot;&gt;# array([2., 5., 8.])  ← 행별 평균&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

  &lt;div style=&quot;background:#fffbeb; border-left:4px solid #f59e0b; padding:14px 18px; border-radius:0 8px 8px 0; margin-bottom:24px; font-size:15px;&quot;&gt;
    &lt;strong&gt;주의:&lt;/strong&gt; &lt;code style=&quot;background:#eef2ff; color:#4f46e5; padding:2px 6px; border-radius:4px;&quot;&gt;mean()&lt;/code&gt;은 항상 &lt;code style=&quot;background:#eef2ff; color:#4f46e5; padding:2px 6px; border-radius:4px;&quot;&gt;float&lt;/code&gt;을 반환합니다. &lt;code style=&quot;background:#eef2ff; color:#4f46e5; padding:2px 6px; border-radius:4px;&quot;&gt;sum()&lt;/code&gt;은 입력 배열의 dtype을 그대로 따릅니다.
  &lt;/div&gt;

  &lt;br/&gt;

  &lt;h2 style=&quot;font-size: 22px; font-weight: 700; color: #1a1a1a; margin-top: 40px; margin-bottom: 16px;&quot;&gt;&lt;code style=&quot;background:#eef2ff; color:#4f46e5; padding:2px 8px; border-radius:4px; font-size:85%;&quot;&gt;keepdims&lt;/code&gt; 옵션 — shape 유지&lt;/h2&gt;

  &lt;p style=&quot;margin: 0 0 16px;&quot;&gt;연산 후에도 원본과 같은 차원 수를 유지합니다. 브로드캐스팅과 함께 쓸 때 유용합니다.&lt;/p&gt;

  &lt;pre style=&quot;background:#1e1e2e; color:#cdd6f4; padding:20px 24px; border-radius:8px; font-size:14px; line-height:1.7; overflow-x:auto; margin-bottom:24px;&quot;&gt;&lt;code&gt;np.sum(a, axis=1)
&lt;span style=&quot;color:#6c7086;&quot;&gt;# array([ 6, 15, 24])  ← shape (3,)  1차원으로 축소됨&lt;/span&gt;

np.sum(a, axis=1, keepdims=True)
&lt;span style=&quot;color:#6c7086;&quot;&gt;# array([[ 6],         ← shape (3, 1)  2차원 유지&lt;/span&gt;
&lt;span style=&quot;color:#6c7086;&quot;&gt;#        [15],&lt;/span&gt;
&lt;span style=&quot;color:#6c7086;&quot;&gt;#        [24]])&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

  &lt;br/&gt;

  &lt;h2 style=&quot;font-size: 22px; font-weight: 700; color: #1a1a1a; margin-top: 40px; margin-bottom: 16px;&quot;&gt;응용 사례 1 — 학생 성적 처리&lt;/h2&gt;

  &lt;pre style=&quot;background:#1e1e2e; color:#cdd6f4; padding:20px 24px; border-radius:8px; font-size:14px; line-height:1.7; overflow-x:auto; margin-bottom:24px;&quot;&gt;&lt;code&gt;&lt;span style=&quot;color:#6c7086;&quot;&gt;# shape: (학생수, 과목수)&lt;/span&gt;
scores = np.array([[85, 90, 78],
                   [70, 65, 88],
                   [92, 88, 95]])

student_avg = np.mean(scores, axis=1)  &lt;span style=&quot;color:#6c7086;&quot;&gt;# 학생별 평균 → [84.3, 74.3, 91.7]&lt;/span&gt;
subject_avg = np.mean(scores, axis=0)  &lt;span style=&quot;color:#6c7086;&quot;&gt;# 과목별 평균 → [82.3, 81.0, 87.0]&lt;/span&gt;
total       = np.sum(scores)           &lt;span style=&quot;color:#6c7086;&quot;&gt;# 전체 합계   → 751&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

  &lt;br/&gt;

  &lt;h2 style=&quot;font-size: 22px; font-weight: 700; color: #1a1a1a; margin-top: 40px; margin-bottom: 16px;&quot;&gt;응용 사례 2 — 이미지 흑백 변환&lt;/h2&gt;

  &lt;pre style=&quot;background:#1e1e2e; color:#cdd6f4; padding:20px 24px; border-radius:8px; font-size:14px; line-height:1.7; overflow-x:auto; margin-bottom:24px;&quot;&gt;&lt;code&gt;&lt;span style=&quot;color:#6c7086;&quot;&gt;# RGB 이미지: shape (H, W, 3)&lt;/span&gt;
img_rgb = np.random.randint(0, 256, (100, 100, 3))

gray = np.mean(img_rgb, axis=2)  &lt;span style=&quot;color:#6c7086;&quot;&gt;# R, G, B 평균 → shape (100, 100)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

  &lt;br/&gt;

  &lt;h2 style=&quot;font-size: 22px; font-weight: 700; color: #1a1a1a; margin-top: 40px; margin-bottom: 16px;&quot;&gt;응용 사례 3 — 정규화 (Mean Centering)&lt;/h2&gt;

  &lt;p style=&quot;margin: 0 0 16px;&quot;&gt;각 feature(열)의 평균을 빼서 데이터를 중앙 정렬합니다. 이때 &lt;code style=&quot;background:#eef2ff; color:#4f46e5; padding:2px 8px; border-radius:4px; font-size:90%;&quot;&gt;keepdims&lt;/code&gt; 없이도 브로드캐스팅이 자동 적용됩니다.&lt;/p&gt;

  &lt;pre style=&quot;background:#1e1e2e; color:#cdd6f4; padding:20px 24px; border-radius:8px; font-size:14px; line-height:1.7; overflow-x:auto; margin-bottom:24px;&quot;&gt;&lt;code&gt;data = np.array([[10, 20, 30],
                 [40, 50, 60]])

col_mean = np.mean(data, axis=0)   &lt;span style=&quot;color:#6c7086;&quot;&gt;# [25., 35., 45.]&lt;/span&gt;
centered = data - col_mean         &lt;span style=&quot;color:#6c7086;&quot;&gt;# 브로드캐스팅 자동 적용&lt;/span&gt;
&lt;span style=&quot;color:#6c7086;&quot;&gt;# array([[-15., -15., -15.],&lt;/span&gt;
&lt;span style=&quot;color:#6c7086;&quot;&gt;#        [ 15.,  15.,  15.]])&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

  &lt;br/&gt;

  &lt;h2 style=&quot;font-size: 22px; font-weight: 700; color: #1a1a1a; margin-top: 40px; margin-bottom: 16px;&quot;&gt;NaN 처리 — &lt;code style=&quot;background:#eef2ff; color:#4f46e5; padding:2px 8px; border-radius:4px; font-size:85%;&quot;&gt;np.nansum()&lt;/code&gt; / &lt;code style=&quot;background:#eef2ff; color:#4f46e5; padding:2px 8px; border-radius:4px; font-size:85%;&quot;&gt;np.nanmean()&lt;/code&gt;&lt;/h2&gt;

  &lt;p style=&quot;margin: 0 0 16px;&quot;&gt;기본 &lt;code style=&quot;background:#eef2ff; color:#4f46e5; padding:2px 8px; border-radius:4px; font-size:90%;&quot;&gt;sum()&lt;/code&gt; / &lt;code style=&quot;background:#eef2ff; color:#4f46e5; padding:2px 8px; border-radius:4px; font-size:90%;&quot;&gt;mean()&lt;/code&gt;은 NaN이 하나라도 있으면 결과가 NaN이 됩니다. &lt;code style=&quot;background:#eef2ff; color:#4f46e5; padding:2px 8px; border-radius:4px; font-size:90%;&quot;&gt;nan&lt;/code&gt; 버전은 NaN을 무시하고 계산합니다.&lt;/p&gt;

  &lt;pre style=&quot;background:#1e1e2e; color:#cdd6f4; padding:20px 24px; border-radius:8px; font-size:14px; line-height:1.7; overflow-x:auto; margin-bottom:24px;&quot;&gt;&lt;code&gt;data = np.array([1.0, 2.0, np.nan, 4.0])

np.sum(data)      &lt;span style=&quot;color:#6c7086;&quot;&gt;# nan   ← NaN 전파&lt;/span&gt;
np.nansum(data)   &lt;span style=&quot;color:#6c7086;&quot;&gt;# 7.0   ← NaN 무시&lt;/span&gt;

np.mean(data)     &lt;span style=&quot;color:#6c7086;&quot;&gt;# nan   ← NaN 전파&lt;/span&gt;
np.nanmean(data)  &lt;span style=&quot;color:#6c7086;&quot;&gt;# 2.33  ← NaN 무시 (1+2+4) / 3&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

  &lt;br/&gt;

  &lt;h2 style=&quot;font-size: 22px; font-weight: 700; color: #1a1a1a; margin-top: 40px; margin-bottom: 16px;&quot;&gt;핵심 요약&lt;/h2&gt;

  &lt;table style=&quot;width:100%; border-collapse:collapse; margin-bottom:24px; font-size:15px;&quot;&gt;
    &lt;thead&gt;
      &lt;tr style=&quot;background:#4f8ef7; color:#fff;&quot;&gt;
        &lt;th style=&quot;padding:10px 16px; text-align:left; border:1px solid #ddd;&quot;&gt;&lt;/th&gt;
        &lt;th style=&quot;padding:10px 16px; text-align:left; border:1px solid #ddd;&quot;&gt;&lt;code style=&quot;background:rgba(255,255,255,0.2); padding:2px 6px; border-radius:4px; font-size:90%;&quot;&gt;np.sum()&lt;/code&gt;&lt;/th&gt;
        &lt;th style=&quot;padding:10px 16px; text-align:left; border:1px solid #ddd;&quot;&gt;&lt;code style=&quot;background:rgba(255,255,255,0.2); padding:2px 6px; border-radius:4px; font-size:90%;&quot;&gt;np.mean()&lt;/code&gt;&lt;/th&gt;
      &lt;/tr&gt;
    &lt;/thead&gt;
    &lt;tbody&gt;
      &lt;tr style=&quot;background:#f9fafb;&quot;&gt;
        &lt;td style=&quot;padding:10px 16px; border:1px solid #ddd;&quot;&gt;반환값&lt;/td&gt;
        &lt;td style=&quot;padding:10px 16px; border:1px solid #ddd;&quot;&gt;합계&lt;/td&gt;
        &lt;td style=&quot;padding:10px 16px; border:1px solid #ddd;&quot;&gt;평균 (항상 float)&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
        &lt;td style=&quot;padding:10px 16px; border:1px solid #ddd;&quot;&gt;axis 없음&lt;/td&gt;
        &lt;td style=&quot;padding:10px 16px; border:1px solid #ddd;&quot;&gt;전체 합산&lt;/td&gt;
        &lt;td style=&quot;padding:10px 16px; border:1px solid #ddd;&quot;&gt;전체 평균&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr style=&quot;background:#f9fafb;&quot;&gt;
        &lt;td style=&quot;padding:10px 16px; border:1px solid #ddd;&quot;&gt;axis=0&lt;/td&gt;
        &lt;td style=&quot;padding:10px 16px; border:1px solid #ddd;&quot;&gt;열 방향 합산 (↓)&lt;/td&gt;
        &lt;td style=&quot;padding:10px 16px; border:1px solid #ddd;&quot;&gt;열 방향 평균 (↓)&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
        &lt;td style=&quot;padding:10px 16px; border:1px solid #ddd;&quot;&gt;axis=1&lt;/td&gt;
        &lt;td style=&quot;padding:10px 16px; border:1px solid #ddd;&quot;&gt;행 방향 합산 (→)&lt;/td&gt;
        &lt;td style=&quot;padding:10px 16px; border:1px solid #ddd;&quot;&gt;행 방향 평균 (→)&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr style=&quot;background:#f9fafb;&quot;&gt;
        &lt;td style=&quot;padding:10px 16px; border:1px solid #ddd;&quot;&gt;NaN 처리&lt;/td&gt;
        &lt;td style=&quot;padding:10px 16px; border:1px solid #ddd;&quot;&gt;&lt;code style=&quot;background:#eef2ff; color:#4f46e5; padding:2px 6px; border-radius:4px;&quot;&gt;np.nansum()&lt;/code&gt;&lt;/td&gt;
        &lt;td style=&quot;padding:10px 16px; border:1px solid #ddd;&quot;&gt;&lt;code style=&quot;background:#eef2ff; color:#4f46e5; padding:2px 6px; border-radius:4px;&quot;&gt;np.nanmean()&lt;/code&gt;&lt;/td&gt;
      &lt;/tr&gt;
    &lt;/tbody&gt;
  &lt;/table&gt;

  &lt;div style=&quot;background:#fffbeb; border-left:4px solid #f59e0b; padding:14px 18px; border-radius:0 8px 8px 0; margin-bottom:24px; font-size:15px;&quot;&gt;
    &lt;strong&gt;핵심 직관:&lt;/strong&gt; &lt;code style=&quot;background:#eef2ff; color:#4f46e5; padding:2px 6px; border-radius:4px;&quot;&gt;axis&lt;/code&gt;는 &quot;이 방향을 &lt;strong&gt;따라서&lt;/strong&gt; 연산한다&quot;는 뜻입니다. &lt;code style=&quot;background:#eef2ff; color:#4f46e5; padding:2px 6px; border-radius:4px;&quot;&gt;axis=0&lt;/code&gt;은 행을 따라 내려가며(→ 열별 결과), &lt;code style=&quot;background:#eef2ff; color:#4f46e5; padding:2px 6px; border-radius:4px;&quot;&gt;axis=1&lt;/code&gt;은 열을 따라 옆으로 가며(→ 행별 결과) 계산합니다.
  &lt;/div&gt;

&lt;/div&gt;</description>
      <category>Python/Python numpy</category>
      <category>Mean</category>
      <category>numpy</category>
      <category>Python</category>
      <category>SUM</category>
      <author>CosmosProject</author>
      <guid isPermaLink="true">https://cosmosproject.tistory.com/938</guid>
      <comments>https://cosmosproject.tistory.com/938#entry938comment</comments>
      <pubDate>Wed, 20 May 2026 21:30:58 +0900</pubDate>
    </item>
    <item>
      <title>Python numpy : minimum() / maximum() &amp;mdash; 두 array를 element-wise로 비교해 작은/큰 값을 선택하는 메서드</title>
      <link>https://cosmosproject.tistory.com/937</link>
      <description>&lt;div style=&quot;font-family: 'Noto Sans KR', Apple SD Gothic Neo, sans-serif; font-size: 16px; line-height: 1.8; color: #333; max-width: 780px; margin: 0 auto;&quot;&gt;

  &lt;h1 style=&quot;font-size: 28px; font-weight: 700; color: #1a1a1a; border-bottom: 3px solid #4f8ef7; padding-bottom: 12px; margin-bottom: 32px;&quot;&gt;NumPy &lt;code style=&quot;background:#eef2ff; color:#4f46e5; padding:2px 8px; border-radius:4px; font-size:90%;&quot;&gt;minimum()&lt;/code&gt; / &lt;code style=&quot;background:#eef2ff; color:#4f46e5; padding:2px 8px; border-radius:4px; font-size:90%;&quot;&gt;maximum()&lt;/code&gt; — 두 array를 element-wise로 비교해 작은/큰 값을 선택하는 메서드&lt;/h1&gt;

  &lt;p style=&quot;margin: 0 0 24px;&quot;&gt;두 array의 같은 위치 값을 비교해서, 더 작은(또는 큰) 값만 골라 새 array를 만듭니다.&lt;/p&gt;

  &lt;h2 style=&quot;font-size: 22px; font-weight: 700; color: #1a1a1a; margin-top: 40px; margin-bottom: 16px;&quot;&gt;기본 문법&lt;/h2&gt;

  &lt;pre style=&quot;background:#1e1e2e; color:#cdd6f4; padding:20px 24px; border-radius:8px; font-size:14px; line-height:1.7; overflow-x:auto; margin-bottom:24px;&quot;&gt;&lt;code&gt;numpy.minimum(x1, x2)   &lt;span style=&quot;color:#6c7086;&quot;&gt;# 각 위치에서 더 작은 값 선택&lt;/span&gt;
numpy.maximum(x1, x2)   &lt;span style=&quot;color:#6c7086;&quot;&gt;# 각 위치에서 더 큰 값 선택&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

  &lt;br/&gt;

  &lt;h2 style=&quot;font-size: 22px; font-weight: 700; color: #1a1a1a; margin-top: 40px; margin-bottom: 16px;&quot;&gt;&lt;code style=&quot;background:#eef2ff; color:#4f46e5; padding:2px 8px; border-radius:4px; font-size:85%;&quot;&gt;np.min()&lt;/code&gt; vs &lt;code style=&quot;background:#eef2ff; color:#4f46e5; padding:2px 8px; border-radius:4px; font-size:85%;&quot;&gt;np.minimum()&lt;/code&gt; — 헷갈리기 쉬운 차이&lt;/h2&gt;

  &lt;p style=&quot;margin: 0 0 16px;&quot;&gt;이름이 비슷하지만 완전히 다릅니다.&lt;/p&gt;

  &lt;pre style=&quot;background:#1e1e2e; color:#cdd6f4; padding:20px 24px; border-radius:8px; font-size:14px; line-height:1.7; overflow-x:auto; margin-bottom:16px;&quot;&gt;&lt;code&gt;arr = np.array([3, 1, 4, 1, 5])

np.min(arr)         &lt;span style=&quot;color:#6c7086;&quot;&gt;# 1  ← array 전체에서 최솟값 하나 반환&lt;/span&gt;
np.minimum(arr, 3)  &lt;span style=&quot;color:#6c7086;&quot;&gt;# array([3, 1, 3, 1, 3])  ← 각 위치를 3과 비교&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

  &lt;table style=&quot;width:100%; border-collapse:collapse; margin-bottom:24px; font-size:15px;&quot;&gt;
    &lt;thead&gt;
      &lt;tr style=&quot;background:#4f8ef7; color:#fff;&quot;&gt;
        &lt;th style=&quot;padding:10px 16px; text-align:left; border:1px solid #ddd;&quot;&gt;&lt;/th&gt;
        &lt;th style=&quot;padding:10px 16px; text-align:left; border:1px solid #ddd;&quot;&gt;&lt;code style=&quot;background:rgba(255,255,255,0.2); padding:2px 6px; border-radius:4px; font-size:90%;&quot;&gt;np.min(arr)&lt;/code&gt;&lt;/th&gt;
        &lt;th style=&quot;padding:10px 16px; text-align:left; border:1px solid #ddd;&quot;&gt;&lt;code style=&quot;background:rgba(255,255,255,0.2); padding:2px 6px; border-radius:4px; font-size:90%;&quot;&gt;np.minimum(x1, x2)&lt;/code&gt;&lt;/th&gt;
      &lt;/tr&gt;
    &lt;/thead&gt;
    &lt;tbody&gt;
      &lt;tr style=&quot;background:#f9fafb;&quot;&gt;
        &lt;td style=&quot;padding:10px 16px; border:1px solid #ddd;&quot;&gt;입력&lt;/td&gt;
        &lt;td style=&quot;padding:10px 16px; border:1px solid #ddd;&quot;&gt;array 1개&lt;/td&gt;
        &lt;td style=&quot;padding:10px 16px; border:1px solid #ddd;&quot;&gt;array 2개 (또는 array + scalar)&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
        &lt;td style=&quot;padding:10px 16px; border:1px solid #ddd;&quot;&gt;출력&lt;/td&gt;
        &lt;td style=&quot;padding:10px 16px; border:1px solid #ddd;&quot;&gt;스칼라 값 1개&lt;/td&gt;
        &lt;td style=&quot;padding:10px 16px; border:1px solid #ddd;&quot;&gt;array&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr style=&quot;background:#f9fafb;&quot;&gt;
        &lt;td style=&quot;padding:10px 16px; border:1px solid #ddd;&quot;&gt;역할&lt;/td&gt;
        &lt;td style=&quot;padding:10px 16px; border:1px solid #ddd;&quot;&gt;전체 최솟값 찾기&lt;/td&gt;
        &lt;td style=&quot;padding:10px 16px; border:1px solid #ddd;&quot;&gt;element-wise 비교&lt;/td&gt;
      &lt;/tr&gt;
    &lt;/tbody&gt;
  &lt;/table&gt;

  &lt;br/&gt;

  &lt;h2 style=&quot;font-size: 22px; font-weight: 700; color: #1a1a1a; margin-top: 40px; margin-bottom: 16px;&quot;&gt;예시 1 — 두 array 비교&lt;/h2&gt;

  &lt;pre style=&quot;background:#1e1e2e; color:#cdd6f4; padding:20px 24px; border-radius:8px; font-size:14px; line-height:1.7; overflow-x:auto; margin-bottom:24px;&quot;&gt;&lt;code&gt;&lt;span style=&quot;color:#89b4fa;&quot;&gt;import&lt;/span&gt; numpy &lt;span style=&quot;color:#89b4fa;&quot;&gt;as&lt;/span&gt; np

a = np.array([1, 5, 3, 8, 2])
b = np.array([4, 2, 6, 1, 7])

np.minimum(a, b)
&lt;span style=&quot;color:#6c7086;&quot;&gt;# array([1, 2, 3, 1, 2])&lt;/span&gt;
&lt;span style=&quot;color:#6c7086;&quot;&gt;#  min(1,4) min(5,2) min(3,6) min(8,1) min(2,7)&lt;/span&gt;

np.maximum(a, b)
&lt;span style=&quot;color:#6c7086;&quot;&gt;# array([4, 5, 6, 8, 7])&lt;/span&gt;
&lt;span style=&quot;color:#6c7086;&quot;&gt;#  max(1,4) max(5,2) max(3,6) max(8,1) max(2,7)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

  &lt;br/&gt;

  &lt;h2 style=&quot;font-size: 22px; font-weight: 700; color: #1a1a1a; margin-top: 40px; margin-bottom: 16px;&quot;&gt;예시 2 — scalar와 비교 (상한/하한 설정)&lt;/h2&gt;

  &lt;pre style=&quot;background:#1e1e2e; color:#cdd6f4; padding:20px 24px; border-radius:8px; font-size:14px; line-height:1.7; overflow-x:auto; margin-bottom:24px;&quot;&gt;&lt;code&gt;arr = np.array([-3, 5, -1, 8, 2])

np.maximum(arr, 0)   &lt;span style=&quot;color:#6c7086;&quot;&gt;# 하한 0 설정 (음수 제거)&lt;/span&gt;
&lt;span style=&quot;color:#6c7086;&quot;&gt;# array([0, 5, 0, 8, 2])&lt;/span&gt;

np.minimum(arr, 5)   &lt;span style=&quot;color:#6c7086;&quot;&gt;# 상한 5 설정&lt;/span&gt;
&lt;span style=&quot;color:#6c7086;&quot;&gt;# array([-3, 5, -1, 5, 2])&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

  &lt;br/&gt;

  &lt;h2 style=&quot;font-size: 22px; font-weight: 700; color: #1a1a1a; margin-top: 40px; margin-bottom: 16px;&quot;&gt;&lt;code style=&quot;background:#eef2ff; color:#4f46e5; padding:2px 8px; border-radius:4px; font-size:85%;&quot;&gt;np.clip()&lt;/code&gt;과의 관계&lt;/h2&gt;

  &lt;p style=&quot;margin: 0 0 16px;&quot;&gt;상한과 하한을 동시에 설정할 때 &lt;code style=&quot;background:#eef2ff; color:#4f46e5; padding:2px 8px; border-radius:4px; font-size:90%;&quot;&gt;clip()&lt;/code&gt;이 더 간결하지만, 내부적으로 &lt;code style=&quot;background:#eef2ff; color:#4f46e5; padding:2px 8px; border-radius:4px; font-size:90%;&quot;&gt;minimum&lt;/code&gt;과 &lt;code style=&quot;background:#eef2ff; color:#4f46e5; padding:2px 8px; border-radius:4px; font-size:90%;&quot;&gt;maximum&lt;/code&gt;의 조합입니다.&lt;/p&gt;

  &lt;pre style=&quot;background:#1e1e2e; color:#cdd6f4; padding:20px 24px; border-radius:8px; font-size:14px; line-height:1.7; overflow-x:auto; margin-bottom:24px;&quot;&gt;&lt;code&gt;arr = np.array([-3, 5, -1, 8, 2])

&lt;span style=&quot;color:#6c7086;&quot;&gt;# 아래 두 코드는 같은 결과&lt;/span&gt;
np.clip(arr, 0, 5)
np.minimum(np.maximum(arr, 0), 5)
&lt;span style=&quot;color:#6c7086;&quot;&gt;# array([0, 5, 0, 5, 2])&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

  &lt;br/&gt;

  &lt;h2 style=&quot;font-size: 22px; font-weight: 700; color: #1a1a1a; margin-top: 40px; margin-bottom: 16px;&quot;&gt;NaN 처리&lt;/h2&gt;

  &lt;p style=&quot;margin: 0 0 16px;&quot;&gt;&lt;code style=&quot;background:#eef2ff; color:#4f46e5; padding:2px 8px; border-radius:4px; font-size:90%;&quot;&gt;np.minimum&lt;/code&gt; / &lt;code style=&quot;background:#eef2ff; color:#4f46e5; padding:2px 8px; border-radius:4px; font-size:90%;&quot;&gt;np.maximum&lt;/code&gt;은 NaN이 있으면 결과도 NaN이 됩니다. NaN을 무시하려면 &lt;code style=&quot;background:#eef2ff; color:#4f46e5; padding:2px 8px; border-radius:4px; font-size:90%;&quot;&gt;np.fmin&lt;/code&gt; / &lt;code style=&quot;background:#eef2ff; color:#4f46e5; padding:2px 8px; border-radius:4px; font-size:90%;&quot;&gt;np.fmax&lt;/code&gt;를 씁니다.&lt;/p&gt;

  &lt;pre style=&quot;background:#1e1e2e; color:#cdd6f4; padding:20px 24px; border-radius:8px; font-size:14px; line-height:1.7; overflow-x:auto; margin-bottom:24px;&quot;&gt;&lt;code&gt;a = np.array([1, np.nan, 3])
b = np.array([2, 2,      1])

np.minimum(a, b)
&lt;span style=&quot;color:#6c7086;&quot;&gt;# array([ 1., nan,  1.])  ← NaN이 있으면 결과도 NaN&lt;/span&gt;

np.fmin(a, b)
&lt;span style=&quot;color:#6c7086;&quot;&gt;# array([1., 2., 1.])     ← NaN 무시하고 다른 값 선택&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

  &lt;br/&gt;

  &lt;h2 style=&quot;font-size: 22px; font-weight: 700; color: #1a1a1a; margin-top: 40px; margin-bottom: 16px;&quot;&gt;&lt;code style=&quot;background:#eef2ff; color:#4f46e5; padding:2px 8px; border-radius:4px; font-size:85%;&quot;&gt;np.fmin()&lt;/code&gt; / &lt;code style=&quot;background:#eef2ff; color:#4f46e5; padding:2px 8px; border-radius:4px; font-size:85%;&quot;&gt;np.fmax()&lt;/code&gt; — NaN을 무시하는 버전&lt;/h2&gt;

  &lt;p style=&quot;margin: 0 0 16px;&quot;&gt;&lt;code style=&quot;background:#eef2ff; color:#4f46e5; padding:2px 8px; border-radius:4px; font-size:90%;&quot;&gt;np.minimum&lt;/code&gt; / &lt;code style=&quot;background:#eef2ff; color:#4f46e5; padding:2px 8px; border-radius:4px; font-size:90%;&quot;&gt;np.maximum&lt;/code&gt;과 동작이 같지만, NaN을 결측값으로 취급해서 무시합니다.&lt;/p&gt;

  &lt;pre style=&quot;background:#1e1e2e; color:#cdd6f4; padding:20px 24px; border-radius:8px; font-size:14px; line-height:1.7; overflow-x:auto; margin-bottom:16px;&quot;&gt;&lt;code&gt;a = np.array([1, np.nan, 3, np.nan])
b = np.array([2, 2,      1, np.nan])

np.minimum(a, b)
&lt;span style=&quot;color:#6c7086;&quot;&gt;# array([ 1., nan,  1., nan])  ← NaN이 하나라도 있으면 결과도 NaN&lt;/span&gt;

np.fmin(a, b)
&lt;span style=&quot;color:#6c7086;&quot;&gt;# array([ 1.,  2.,  1., nan])  ← 한쪽만 NaN이면 다른 값 선택&lt;/span&gt;
&lt;span style=&quot;color:#6c7086;&quot;&gt;#                        ↑ 둘 다 NaN이면 그때만 NaN&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

  &lt;div style=&quot;background:#fffbeb; border-left:4px solid #f59e0b; padding:14px 18px; border-radius:0 8px 8px 0; margin-bottom:16px; font-size:15px;&quot;&gt;
    &lt;strong&gt;NaN 처리 규칙:&lt;/strong&gt; 한쪽만 NaN → NaN이 아닌 값 선택 / 둘 다 NaN → NaN 반환
  &lt;/div&gt;

  &lt;p style=&quot;margin: 0 0 16px;&quot;&gt;데이터에 결측값이 섞여 있을 때, NaN 때문에 비교 결과 전체가 오염되는 걸 막고 싶을 때 씁니다.&lt;/p&gt;

  &lt;pre style=&quot;background:#1e1e2e; color:#cdd6f4; padding:20px 24px; border-radius:8px; font-size:14px; line-height:1.7; overflow-x:auto; margin-bottom:24px;&quot;&gt;&lt;code&gt;&lt;span style=&quot;color:#6c7086;&quot;&gt;# 센서 데이터 — 일부 측정값이 누락(NaN)된 상황&lt;/span&gt;
sensor_a = np.array([23.1, np.nan, 19.8, 21.0])
sensor_b = np.array([22.0, 20.5,   np.nan, 21.5])

&lt;span style=&quot;color:#6c7086;&quot;&gt;# 두 센서 중 낮은 값을 기록하되, 한쪽이 누락이면 다른 쪽 값 사용&lt;/span&gt;
np.fmin(sensor_a, sensor_b)
&lt;span style=&quot;color:#6c7086;&quot;&gt;# array([22. , 20.5, 19.8, 21. ])&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

  &lt;table style=&quot;width:100%; border-collapse:collapse; margin-bottom:24px; font-size:15px;&quot;&gt;
    &lt;thead&gt;
      &lt;tr style=&quot;background:#4f8ef7; color:#fff;&quot;&gt;
        &lt;th style=&quot;padding:10px 16px; text-align:left; border:1px solid #ddd;&quot;&gt;&lt;/th&gt;
        &lt;th style=&quot;padding:10px 16px; text-align:left; border:1px solid #ddd;&quot;&gt;&lt;code style=&quot;background:rgba(255,255,255,0.2); padding:2px 6px; border-radius:4px; font-size:90%;&quot;&gt;np.minimum&lt;/code&gt;&lt;/th&gt;
        &lt;th style=&quot;padding:10px 16px; text-align:left; border:1px solid #ddd;&quot;&gt;&lt;code style=&quot;background:rgba(255,255,255,0.2); padding:2px 6px; border-radius:4px; font-size:90%;&quot;&gt;np.fmin&lt;/code&gt;&lt;/th&gt;
      &lt;/tr&gt;
    &lt;/thead&gt;
    &lt;tbody&gt;
      &lt;tr style=&quot;background:#f9fafb;&quot;&gt;
        &lt;td style=&quot;padding:10px 16px; border:1px solid #ddd;&quot;&gt;한쪽 NaN&lt;/td&gt;
        &lt;td style=&quot;padding:10px 16px; border:1px solid #ddd;&quot;&gt;NaN 반환&lt;/td&gt;
        &lt;td style=&quot;padding:10px 16px; border:1px solid #ddd;&quot;&gt;다른 값 반환&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
        &lt;td style=&quot;padding:10px 16px; border:1px solid #ddd;&quot;&gt;둘 다 NaN&lt;/td&gt;
        &lt;td style=&quot;padding:10px 16px; border:1px solid #ddd;&quot;&gt;NaN 반환&lt;/td&gt;
        &lt;td style=&quot;padding:10px 16px; border:1px solid #ddd;&quot;&gt;NaN 반환&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr style=&quot;background:#f9fafb;&quot;&gt;
        &lt;td style=&quot;padding:10px 16px; border:1px solid #ddd;&quot;&gt;용도&lt;/td&gt;
        &lt;td style=&quot;padding:10px 16px; border:1px solid #ddd;&quot;&gt;NaN 전파가 필요할 때&lt;/td&gt;
        &lt;td style=&quot;padding:10px 16px; border:1px solid #ddd;&quot;&gt;NaN을 무시하고 싶을 때&lt;/td&gt;
      &lt;/tr&gt;
    &lt;/tbody&gt;
  &lt;/table&gt;

&lt;/div&gt;</description>
      <category>Python/Python numpy</category>
      <category>fmax</category>
      <category>fmin</category>
      <category>Maximum</category>
      <category>minimum</category>
      <category>numpy</category>
      <category>Python</category>
      <author>CosmosProject</author>
      <guid isPermaLink="true">https://cosmosproject.tistory.com/937</guid>
      <comments>https://cosmosproject.tistory.com/937#entry937comment</comments>
      <pubDate>Thu, 14 May 2026 20:21:56 +0900</pubDate>
    </item>
    <item>
      <title>Python numpy : where() &amp;mdash; 조건에 따라 두 값 중 하나를 선택하는 메서드</title>
      <link>https://cosmosproject.tistory.com/936</link>
      <description>&lt;div style=&quot;font-family: 'Noto Sans KR', Apple SD Gothic Neo, sans-serif; font-size: 16px; line-height: 1.8; color: #333; max-width: 780px; margin: 0 auto;&quot;&gt;

  &lt;h1 style=&quot;font-size: 28px; font-weight: 700; color: #1a1a1a; border-bottom: 3px solid #4f8ef7; padding-bottom: 12px; margin-bottom: 32px;&quot;&gt;NumPy &lt;code style=&quot;background:#eef2ff; color:#4f46e5; padding:2px 8px; border-radius:4px; font-size:90%;&quot;&gt;where()&lt;/code&gt; — 조건에 따라 두 값 중 하나를 선택하는 메서드&lt;/h1&gt;

  &lt;p style=&quot;margin: 0 0 24px;&quot;&gt;조건이 &lt;code style=&quot;background:#eef2ff; color:#4f46e5; padding:2px 8px; border-radius:4px; font-size:90%;&quot;&gt;True&lt;/code&gt;인 위치에는 x값, &lt;code style=&quot;background:#eef2ff; color:#4f46e5; padding:2px 8px; border-radius:4px; font-size:90%;&quot;&gt;False&lt;/code&gt;인 위치에는 y값을 선택해서 새 array를 만듭니다.&lt;/p&gt;

  &lt;h2 style=&quot;font-size: 22px; font-weight: 700; color: #1a1a1a; margin-top: 40px; margin-bottom: 16px;&quot;&gt;기본 문법&lt;/h2&gt;

  &lt;pre style=&quot;background:#1e1e2e; color:#cdd6f4; padding:20px 24px; border-radius:8px; font-size:14px; line-height:1.7; overflow-x:auto; margin-bottom:24px;&quot;&gt;&lt;code&gt;&lt;span style=&quot;color:#6c7086;&quot;&gt;# 형태 1 — 조건에 따라 값 선택 (가장 많이 쓰는 형태)&lt;/span&gt;
numpy.where(condition, x, y)

&lt;span style=&quot;color:#6c7086;&quot;&gt;# 형태 2 — 조건이 True인 인덱스 반환&lt;/span&gt;
numpy.where(condition)&lt;/code&gt;&lt;/pre&gt;

  &lt;br/&gt;

  &lt;h2 style=&quot;font-size: 22px; font-weight: 700; color: #1a1a1a; margin-top: 40px; margin-bottom: 16px;&quot;&gt;형태 1 — 조건에 따라 값 선택&lt;/h2&gt;

  &lt;pre style=&quot;background:#1e1e2e; color:#cdd6f4; padding:20px 24px; border-radius:8px; font-size:14px; line-height:1.7; overflow-x:auto; margin-bottom:16px;&quot;&gt;&lt;code&gt;&lt;span style=&quot;color:#89b4fa;&quot;&gt;import&lt;/span&gt; numpy &lt;span style=&quot;color:#89b4fa;&quot;&gt;as&lt;/span&gt; np

arr = np.array([1, -2, 3, -4, 5])

np.where(arr &gt; 0, arr, 0)
&lt;span style=&quot;color:#6c7086;&quot;&gt;# array([1, 0, 3, 0, 5])&lt;/span&gt;
&lt;span style=&quot;color:#6c7086;&quot;&gt;#           ↑     ↑&lt;/span&gt;
&lt;span style=&quot;color:#6c7086;&quot;&gt;#        -2, -4 → 0으로 대체&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

  &lt;p style=&quot;margin: 0 0 24px;&quot;&gt;조건이 &lt;code style=&quot;background:#eef2ff; color:#4f46e5; padding:2px 8px; border-radius:4px; font-size:90%;&quot;&gt;True&lt;/code&gt;인 위치 → &lt;code style=&quot;background:#eef2ff; color:#4f46e5; padding:2px 8px; border-radius:4px; font-size:90%;&quot;&gt;arr&lt;/code&gt; 값 그대로, &lt;code style=&quot;background:#eef2ff; color:#4f46e5; padding:2px 8px; border-radius:4px; font-size:90%;&quot;&gt;False&lt;/code&gt;인 위치 → &lt;code style=&quot;background:#eef2ff; color:#4f46e5; padding:2px 8px; border-radius:4px; font-size:90%;&quot;&gt;0&lt;/code&gt;&lt;/p&gt;

  &lt;br/&gt;

  &lt;h2 style=&quot;font-size: 22px; font-weight: 700; color: #1a1a1a; margin-top: 40px; margin-bottom: 16px;&quot;&gt;x, y 자리에 값 대신 array도 가능&lt;/h2&gt;

  &lt;pre style=&quot;background:#1e1e2e; color:#cdd6f4; padding:20px 24px; border-radius:8px; font-size:14px; line-height:1.7; overflow-x:auto; margin-bottom:24px;&quot;&gt;&lt;code&gt;a = np.array([1, 2, 3, 4, 5])
b = np.array([10, 20, 30, 40, 50])

np.where(a &gt; 3, a, b)
&lt;span style=&quot;color:#6c7086;&quot;&gt;# array([10, 20, 30,  4,  5])&lt;/span&gt;
&lt;span style=&quot;color:#6c7086;&quot;&gt;#         ↑   ↑   ↑   ↑   ↑&lt;/span&gt;
&lt;span style=&quot;color:#6c7086;&quot;&gt;# 1≤3→b  2≤3→b  3≤3→b  4&gt;3→a  5&gt;3→a&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

  &lt;br/&gt;

  &lt;h2 style=&quot;font-size: 22px; font-weight: 700; color: #1a1a1a; margin-top: 40px; margin-bottom: 16px;&quot;&gt;형태 2 — True인 인덱스 반환&lt;/h2&gt;

  &lt;p style=&quot;margin: 0 0 16px;&quot;&gt;인수를 하나만 넘기면 조건을 만족하는 위치의 인덱스를 반환합니다.&lt;/p&gt;

  &lt;pre style=&quot;background:#1e1e2e; color:#cdd6f4; padding:20px 24px; border-radius:8px; font-size:14px; line-height:1.7; overflow-x:auto; margin-bottom:24px;&quot;&gt;&lt;code&gt;arr = np.array([10, 50, 30, 80, 20])

np.where(arr &gt; 40)
&lt;span style=&quot;color:#6c7086;&quot;&gt;# (array([1, 3]),)  ← 인덱스 1, 3이 조건 만족 (50, 80)&lt;/span&gt;

&lt;span style=&quot;color:#6c7086;&quot;&gt;# 인덱스로 값 추출할 때&lt;/span&gt;
arr[np.where(arr &gt; 40)]
&lt;span style=&quot;color:#6c7086;&quot;&gt;# array([50, 80])&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

  &lt;br/&gt;

  &lt;h2 style=&quot;font-size: 22px; font-weight: 700; color: #1a1a1a; margin-top: 40px; margin-bottom: 16px;&quot;&gt;&lt;code style=&quot;background:#eef2ff; color:#4f46e5; padding:2px 8px; border-radius:4px; font-size:85%;&quot;&gt;pandas.where&lt;/code&gt; vs &lt;code style=&quot;background:#eef2ff; color:#4f46e5; padding:2px 8px; border-radius:4px; font-size:85%;&quot;&gt;numpy.where&lt;/code&gt; — 헷갈리기 쉬운 차이&lt;/h2&gt;

  &lt;p style=&quot;margin: 0 0 16px;&quot;&gt;이름은 같지만 동작 방향이 반대입니다.&lt;/p&gt;

  &lt;pre style=&quot;background:#1e1e2e; color:#cdd6f4; padding:20px 24px; border-radius:8px; font-size:14px; line-height:1.7; overflow-x:auto; margin-bottom:16px;&quot;&gt;&lt;code&gt;s = pd.Series([1, -2, 3, -4, 5])

&lt;span style=&quot;color:#6c7086;&quot;&gt;# pandas.where → True면 원래 값 유지, False면 대체&lt;/span&gt;
s.where(s &gt; 0, 0)
&lt;span style=&quot;color:#6c7086;&quot;&gt;# [1, 0, 3, 0, 5]   ← 양수는 유지, 음수는 0&lt;/span&gt;

&lt;span style=&quot;color:#6c7086;&quot;&gt;# numpy.where → True면 x, False면 y&lt;/span&gt;
np.where(s &gt; 0, s, 0)
&lt;span style=&quot;color:#6c7086;&quot;&gt;# [1, 0, 3, 0, 5]   ← 결과는 같지만 사고방식이 다름&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

  &lt;table style=&quot;width:100%; border-collapse:collapse; margin-bottom:24px; font-size:15px;&quot;&gt;
    &lt;thead&gt;
      &lt;tr style=&quot;background:#4f8ef7; color:#fff;&quot;&gt;
        &lt;th style=&quot;padding:10px 16px; text-align:left; border:1px solid #ddd;&quot;&gt;&lt;/th&gt;
        &lt;th style=&quot;padding:10px 16px; text-align:left; border:1px solid #ddd;&quot;&gt;&lt;code style=&quot;background:rgba(255,255,255,0.2); padding:2px 6px; border-radius:4px; font-size:90%;&quot;&gt;pandas.where(cond, other)&lt;/code&gt;&lt;/th&gt;
        &lt;th style=&quot;padding:10px 16px; text-align:left; border:1px solid #ddd;&quot;&gt;&lt;code style=&quot;background:rgba(255,255,255,0.2); padding:2px 6px; border-radius:4px; font-size:90%;&quot;&gt;numpy.where(cond, x, y)&lt;/code&gt;&lt;/th&gt;
      &lt;/tr&gt;
    &lt;/thead&gt;
    &lt;tbody&gt;
      &lt;tr style=&quot;background:#f9fafb;&quot;&gt;
        &lt;td style=&quot;padding:10px 16px; border:1px solid #ddd;&quot;&gt;True일 때&lt;/td&gt;
        &lt;td style=&quot;padding:10px 16px; border:1px solid #ddd;&quot;&gt;원래 값 유지&lt;/td&gt;
        &lt;td style=&quot;padding:10px 16px; border:1px solid #ddd;&quot;&gt;x 선택&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
        &lt;td style=&quot;padding:10px 16px; border:1px solid #ddd;&quot;&gt;False일 때&lt;/td&gt;
        &lt;td style=&quot;padding:10px 16px; border:1px solid #ddd;&quot;&gt;other로 대체&lt;/td&gt;
        &lt;td style=&quot;padding:10px 16px; border:1px solid #ddd;&quot;&gt;y 선택&lt;/td&gt;
      &lt;/tr&gt;
    &lt;/tbody&gt;
  &lt;/table&gt;

&lt;/div&gt;</description>
      <category>Python/Python numpy</category>
      <category>numpy</category>
      <category>Python</category>
      <category>where</category>
      <author>CosmosProject</author>
      <guid isPermaLink="true">https://cosmosproject.tistory.com/936</guid>
      <comments>https://cosmosproject.tistory.com/936#entry936comment</comments>
      <pubDate>Thu, 14 May 2026 20:13:07 +0900</pubDate>
    </item>
    <item>
      <title>Python numpy : polyfit() &amp;mdash; 점들의 패턴에 가장 잘 맞는 다항식 계수를 추정하는 메서드</title>
      <link>https://cosmosproject.tistory.com/935</link>
      <description>&lt;div style=&quot;font-family: 'Noto Sans KR', Apple SD Gothic Neo, sans-serif; font-size: 16px; line-height: 1.8; color: #333; max-width: 780px; margin: 0 auto;&quot;&gt;

  &lt;h1 style=&quot;font-size: 28px; font-weight: 700; color: #1a1a1a; border-bottom: 3px solid #4f8ef7; padding-bottom: 12px; margin-bottom: 32px;&quot;&gt;NumPy &lt;code style=&quot;background:#eef2ff; color:#4f46e5; padding:2px 8px; border-radius:4px; font-size:90%;&quot;&gt;polyfit()&lt;/code&gt; — 점들의 패턴에 가장 잘 맞는 다항식 계수를 추정하는 메서드&lt;/h1&gt;

  &lt;p style=&quot;margin: 0 0 24px;&quot;&gt;여러 (x, y) 좌표 쌍을 받아서, 그 점들을 가장 잘 설명하는 다항식의 계수를 반환합니다.&lt;/p&gt;

  &lt;h2 style=&quot;font-size: 22px; font-weight: 700; color: #1a1a1a; margin-top: 40px; margin-bottom: 16px;&quot;&gt;기본 문법&lt;/h2&gt;

  &lt;pre style=&quot;background:#1e1e2e; color:#cdd6f4; padding:20px 24px; border-radius:8px; font-size:14px; line-height:1.7; overflow-x:auto; margin-bottom:24px;&quot;&gt;&lt;code&gt;numpy.polyfit(x, y, deg)&lt;/code&gt;&lt;/pre&gt;

  &lt;table style=&quot;width:100%; border-collapse:collapse; margin-bottom:16px; font-size:15px;&quot;&gt;
    &lt;thead&gt;
      &lt;tr style=&quot;background:#4f8ef7; color:#fff;&quot;&gt;
        &lt;th style=&quot;padding:10px 16px; text-align:left; border:1px solid #ddd;&quot;&gt;파라미터&lt;/th&gt;
        &lt;th style=&quot;padding:10px 16px; text-align:left; border:1px solid #ddd;&quot;&gt;설명&lt;/th&gt;
      &lt;/tr&gt;
    &lt;/thead&gt;
    &lt;tbody&gt;
      &lt;tr style=&quot;background:#f9fafb;&quot;&gt;
        &lt;td style=&quot;padding:10px 16px; border:1px solid #ddd;&quot;&gt;&lt;code style=&quot;background:#eef2ff; color:#4f46e5; padding:2px 6px; border-radius:4px; font-size:90%;&quot;&gt;x&lt;/code&gt;&lt;/td&gt;
        &lt;td style=&quot;padding:10px 16px; border:1px solid #ddd;&quot;&gt;x 좌표값들의 array&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
        &lt;td style=&quot;padding:10px 16px; border:1px solid #ddd;&quot;&gt;&lt;code style=&quot;background:#eef2ff; color:#4f46e5; padding:2px 6px; border-radius:4px; font-size:90%;&quot;&gt;y&lt;/code&gt;&lt;/td&gt;
        &lt;td style=&quot;padding:10px 16px; border:1px solid #ddd;&quot;&gt;y 좌표값들의 array&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr style=&quot;background:#f9fafb;&quot;&gt;
        &lt;td style=&quot;padding:10px 16px; border:1px solid #ddd;&quot;&gt;&lt;code style=&quot;background:#eef2ff; color:#4f46e5; padding:2px 6px; border-radius:4px; font-size:90%;&quot;&gt;deg&lt;/code&gt;&lt;/td&gt;
        &lt;td style=&quot;padding:10px 16px; border:1px solid #ddd;&quot;&gt;다항식의 차수&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
        &lt;td style=&quot;padding:10px 16px; border:1px solid #ddd;&quot;&gt;반환값&lt;/td&gt;
        &lt;td style=&quot;padding:10px 16px; border:1px solid #ddd;&quot;&gt;계수 array. &lt;strong&gt;높은 차수부터&lt;/strong&gt; 순서대로 반환&lt;/td&gt;
      &lt;/tr&gt;
    &lt;/tbody&gt;
  &lt;/table&gt;

  &lt;p style=&quot;margin: 0 0 12px;&quot;&gt;&lt;code style=&quot;background:#eef2ff; color:#4f46e5; padding:2px 8px; border-radius:4px; font-size:90%;&quot;&gt;deg&lt;/code&gt;에 따라 추정하는 다항식의 형태가 달라집니다.&lt;/p&gt;

  &lt;pre style=&quot;background:#1e1e2e; color:#cdd6f4; padding:20px 24px; border-radius:8px; font-size:14px; line-height:1.7; overflow-x:auto; margin-bottom:24px;&quot;&gt;&lt;code&gt;deg=1  →  y = ax + b                     (직선)
deg=2  →  y = ax² + bx + c              (포물선)
deg=3  →  y = ax³ + bx² + cx + d        (S자 곡선)
deg=4  →  y = ax⁴ + bx³ + cx² + dx + e&lt;/code&gt;&lt;/pre&gt;

  &lt;br/&gt;

  &lt;h2 style=&quot;font-size: 22px; font-weight: 700; color: #1a1a1a; margin-top: 40px; margin-bottom: 16px;&quot;&gt;&quot;가장 잘 맞는&quot;이 뭔 뜻인가 — 최소제곱법&lt;/h2&gt;

  &lt;p style=&quot;margin: 0 0 16px;&quot;&gt;점들이 완벽하게 하나의 선 위에 있는 경우는 거의 없습니다. 대신 각 점과 추정한 곡선 사이의 &lt;strong&gt;오차(거리)의 제곱 합이 최소&lt;/strong&gt;가 되는 계수를 찾습니다. 이걸 최소제곱법(Least Squares)이라 합니다.&lt;/p&gt;

  &lt;pre style=&quot;background:#1e1e2e; color:#cdd6f4; padding:20px 24px; border-radius:8px; font-size:14px; line-height:1.7; overflow-x:auto; margin-bottom:24px;&quot;&gt;&lt;code&gt;실제 y값과 추정 y값의 차이  →  오차
오차² 의 합이 가장 작아지는 계수를 선택&lt;/code&gt;&lt;/pre&gt;

  &lt;br/&gt;

  &lt;h2 style=&quot;font-size: 22px; font-weight: 700; color: #1a1a1a; margin-top: 40px; margin-bottom: 16px;&quot;&gt;예시 1 — 1차 (직선 피팅)&lt;/h2&gt;

  &lt;pre style=&quot;background:#1e1e2e; color:#cdd6f4; padding:20px 24px; border-radius:8px; font-size:14px; line-height:1.7; overflow-x:auto; margin-bottom:16px;&quot;&gt;&lt;code&gt;&lt;span style=&quot;color:#89b4fa;&quot;&gt;import&lt;/span&gt; numpy &lt;span style=&quot;color:#89b4fa;&quot;&gt;as&lt;/span&gt; np

x = np.array([1, 2, 3, 4, 5])
y = np.array([2.1, 3.9, 6.2, 7.8, 10.1])
&lt;span style=&quot;color:#6c7086;&quot;&gt;# 대략 y = 2x 패턴&lt;/span&gt;

coeffs = np.polyfit(x, y, deg=1)
&lt;span style=&quot;color:#6c7086;&quot;&gt;# array([2.02, 0.02])&lt;/span&gt;
&lt;span style=&quot;color:#6c7086;&quot;&gt;#         ↑      ↑&lt;/span&gt;
&lt;span style=&quot;color:#6c7086;&quot;&gt;#         a      b   →  y = 2.02x + 0.02&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

  &lt;p style=&quot;margin: 0 0 24px;&quot;&gt;deg=1이면 &lt;code style=&quot;background:#eef2ff; color:#4f46e5; padding:2px 8px; border-radius:4px; font-size:90%;&quot;&gt;y = ax + b&lt;/code&gt; 형태. 반환값은 &lt;code style=&quot;background:#eef2ff; color:#4f46e5; padding:2px 8px; border-radius:4px; font-size:90%;&quot;&gt;[a, b]&lt;/code&gt;.&lt;/p&gt;

  &lt;br/&gt;

  &lt;h2 style=&quot;font-size: 22px; font-weight: 700; color: #1a1a1a; margin-top: 40px; margin-bottom: 16px;&quot;&gt;예시 2 — 2차 (곡선 피팅)&lt;/h2&gt;

  &lt;pre style=&quot;background:#1e1e2e; color:#cdd6f4; padding:20px 24px; border-radius:8px; font-size:14px; line-height:1.7; overflow-x:auto; margin-bottom:16px;&quot;&gt;&lt;code&gt;&lt;span style=&quot;color:#89b4fa;&quot;&gt;import&lt;/span&gt; numpy &lt;span style=&quot;color:#89b4fa;&quot;&gt;as&lt;/span&gt; np

x = np.array([1, 2, 3, 4, 5])
y = np.array([1.1, 3.9, 9.2, 15.8, 25.1])
&lt;span style=&quot;color:#6c7086;&quot;&gt;# 대략 y = x² 패턴&lt;/span&gt;

coeffs = np.polyfit(x, y, deg=2)
&lt;span style=&quot;color:#6c7086;&quot;&gt;# array([0.99, 0.01, 0.05])&lt;/span&gt;
&lt;span style=&quot;color:#6c7086;&quot;&gt;#         ↑      ↑     ↑&lt;/span&gt;
&lt;span style=&quot;color:#6c7086;&quot;&gt;#         a      b     c   →  y = 0.99x² + 0.01x + 0.05&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

  &lt;p style=&quot;margin: 0 0 24px;&quot;&gt;deg=2이면 &lt;code style=&quot;background:#eef2ff; color:#4f46e5; padding:2px 8px; border-radius:4px; font-size:90%;&quot;&gt;y = ax² + bx + c&lt;/code&gt; 형태. 반환값은 &lt;code style=&quot;background:#eef2ff; color:#4f46e5; padding:2px 8px; border-radius:4px; font-size:90%;&quot;&gt;[a, b, c]&lt;/code&gt;.&lt;/p&gt;

  &lt;br/&gt;

  &lt;h2 style=&quot;font-size: 22px; font-weight: 700; color: #1a1a1a; margin-top: 40px; margin-bottom: 16px;&quot;&gt;계수로 예측값 구하기 — &lt;code style=&quot;background:#eef2ff; color:#4f46e5; padding:2px 8px; border-radius:4px; font-size:85%;&quot;&gt;np.polyval()&lt;/code&gt;&lt;/h2&gt;

  &lt;p style=&quot;margin: 0 0 16px;&quot;&gt;&lt;code style=&quot;background:#eef2ff; color:#4f46e5; padding:2px 8px; border-radius:4px; font-size:90%;&quot;&gt;polyfit()&lt;/code&gt;이 계수를 구해줬다면, &lt;code style=&quot;background:#eef2ff; color:#4f46e5; padding:2px 8px; border-radius:4px; font-size:90%;&quot;&gt;np.polyval()&lt;/code&gt;로 새로운 x에 대한 y를 예측할 수 있습니다.&lt;/p&gt;

  &lt;pre style=&quot;background:#1e1e2e; color:#cdd6f4; padding:20px 24px; border-radius:8px; font-size:14px; line-height:1.7; overflow-x:auto; margin-bottom:24px;&quot;&gt;&lt;code&gt;coeffs = np.polyfit(x, y, deg=1)

&lt;span style=&quot;color:#6c7086;&quot;&gt;# x=6일 때 예측값&lt;/span&gt;
np.polyval(coeffs, 6)
&lt;span style=&quot;color:#6c7086;&quot;&gt;# 12.14&lt;/span&gt;

&lt;span style=&quot;color:#6c7086;&quot;&gt;# 여러 x에 대해 한 번에&lt;/span&gt;
np.polyval(coeffs, [6, 7, 8])
&lt;span style=&quot;color:#6c7086;&quot;&gt;# [12.14, 14.16, 16.18]&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

  &lt;br/&gt;

  &lt;h2 style=&quot;font-size: 22px; font-weight: 700; color: #1a1a1a; margin-top: 40px; margin-bottom: 16px;&quot;&gt;차수(deg) 선택 주의&lt;/h2&gt;

  &lt;pre style=&quot;background:#1e1e2e; color:#cdd6f4; padding:20px 24px; border-radius:8px; font-size:14px; line-height:1.7; overflow-x:auto; margin-bottom:16px;&quot;&gt;&lt;code&gt;deg가 너무 낮으면  →  패턴을 못 잡음 (underfitting)
deg가 너무 높으면  →  노이즈까지 외워버림 (overfitting)&lt;/code&gt;&lt;/pre&gt;

  &lt;pre style=&quot;background:#1e1e2e; color:#cdd6f4; padding:20px 24px; border-radius:8px; font-size:14px; line-height:1.7; overflow-x:auto; margin-bottom:16px;&quot;&gt;&lt;code&gt;&lt;span style=&quot;color:#6c7086;&quot;&gt;# 점이 5개인데 deg=4로 하면&lt;/span&gt;
&lt;span style=&quot;color:#6c7086;&quot;&gt;# 모든 점을 정확히 지나는 곡선이 나오지만&lt;/span&gt;
&lt;span style=&quot;color:#6c7086;&quot;&gt;# 실제 패턴과는 거리가 멀어질 수 있음&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

  &lt;div style=&quot;background:#fffbeb; border-left:4px solid #f59e0b; padding:14px 18px; border-radius:0 8px 8px 0; margin-bottom:24px; font-size:15px;&quot;&gt;
    일반적으로 데이터의 실제 관계를 고려해서 &lt;code style=&quot;background:#eef2ff; color:#4f46e5; padding:2px 6px; border-radius:4px; font-size:90%;&quot;&gt;deg=1&lt;/code&gt;(선형) 또는 &lt;code style=&quot;background:#eef2ff; color:#4f46e5; padding:2px 6px; border-radius:4px; font-size:90%;&quot;&gt;deg=2&lt;/code&gt;(2차)를 먼저 시도합니다.
  &lt;/div&gt;

&lt;/div&gt;</description>
      <category>Python/Python numpy</category>
      <category>numpy</category>
      <category>polyfit</category>
      <category>Python</category>
      <author>CosmosProject</author>
      <guid isPermaLink="true">https://cosmosproject.tistory.com/935</guid>
      <comments>https://cosmosproject.tistory.com/935#entry935comment</comments>
      <pubDate>Thu, 14 May 2026 19:58:08 +0900</pubDate>
    </item>
    <item>
      <title>Python Pandas : std() &amp;mdash; 표준편차를 계산하는 메서드</title>
      <link>https://cosmosproject.tistory.com/934</link>
      <description>&lt;div style=&quot;font-family: 'Noto Sans KR', Apple SD Gothic Neo, sans-serif; font-size: 16px; line-height: 1.8; color: #333; max-width: 780px; margin: 0 auto;&quot;&gt;

  &lt;h1 style=&quot;font-size: 28px; font-weight: 700; color: #1a1a1a; border-bottom: 3px solid #4f8ef7; padding-bottom: 12px; margin-bottom: 32px;&quot;&gt;Pandas &lt;code style=&quot;background:#eef2ff; color:#4f46e5; padding:2px 8px; border-radius:4px; font-size:90%;&quot;&gt;.std()&lt;/code&gt; — 표준편차를 계산하는 메서드&lt;/h1&gt;

  &lt;p style=&quot;margin: 0 0 24px;&quot;&gt;데이터가 평균에서 얼마나 퍼져 있는지를 수치로 나타냅니다. 값이 클수록 데이터가 들쭉날쭉하고, 작을수록 고르게 모여 있습니다.&lt;/p&gt;

  &lt;h2 style=&quot;font-size: 22px; font-weight: 700; color: #1a1a1a; margin-top: 40px; margin-bottom: 16px;&quot;&gt;기본 문법&lt;/h2&gt;

  &lt;pre style=&quot;background:#1e1e2e; color:#cdd6f4; padding:20px 24px; border-radius:8px; font-size:14px; line-height:1.7; overflow-x:auto; margin-bottom:24px;&quot;&gt;&lt;code&gt;Series.std(ddof=1)
DataFrame.std(ddof=1, axis=0)&lt;/code&gt;&lt;/pre&gt;

  &lt;table style=&quot;width:100%; border-collapse:collapse; margin-bottom:24px; font-size:15px;&quot;&gt;
    &lt;thead&gt;
      &lt;tr style=&quot;background:#4f8ef7; color:#fff;&quot;&gt;
        &lt;th style=&quot;padding:10px 16px; text-align:left; border:1px solid #ddd;&quot;&gt;파라미터&lt;/th&gt;
        &lt;th style=&quot;padding:10px 16px; text-align:left; border:1px solid #ddd;&quot;&gt;설명&lt;/th&gt;
      &lt;/tr&gt;
    &lt;/thead&gt;
    &lt;tbody&gt;
      &lt;tr style=&quot;background:#f9fafb;&quot;&gt;
        &lt;td style=&quot;padding:10px 16px; border:1px solid #ddd;&quot;&gt;&lt;code style=&quot;background:#eef2ff; color:#4f46e5; padding:2px 6px; border-radius:4px; font-size:90%;&quot;&gt;ddof&lt;/code&gt;&lt;/td&gt;
        &lt;td style=&quot;padding:10px 16px; border:1px solid #ddd;&quot;&gt;기본값 &lt;code style=&quot;background:#eef2ff; color:#4f46e5; padding:2px 6px; border-radius:4px; font-size:90%;&quot;&gt;1&lt;/code&gt;은 표본 표준편차, &lt;code style=&quot;background:#eef2ff; color:#4f46e5; padding:2px 6px; border-radius:4px; font-size:90%;&quot;&gt;0&lt;/code&gt;으로 바꾸면 모 표준편차&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
        &lt;td style=&quot;padding:10px 16px; border:1px solid #ddd;&quot;&gt;&lt;code style=&quot;background:#eef2ff; color:#4f46e5; padding:2px 6px; border-radius:4px; font-size:90%;&quot;&gt;axis&lt;/code&gt;&lt;/td&gt;
        &lt;td style=&quot;padding:10px 16px; border:1px solid #ddd;&quot;&gt;&lt;code style=&quot;background:#eef2ff; color:#4f46e5; padding:2px 6px; border-radius:4px; font-size:90%;&quot;&gt;0&lt;/code&gt;이면 열 방향(각 컬럼의 std), &lt;code style=&quot;background:#eef2ff; color:#4f46e5; padding:2px 6px; border-radius:4px; font-size:90%;&quot;&gt;1&lt;/code&gt;이면 행 방향&lt;/td&gt;
      &lt;/tr&gt;
    &lt;/tbody&gt;
  &lt;/table&gt;

  &lt;br/&gt;

  &lt;h2 style=&quot;font-size: 22px; font-weight: 700; color: #1a1a1a; margin-top: 40px; margin-bottom: 16px;&quot;&gt;예시 1 — Series 기본&lt;/h2&gt;

  &lt;pre style=&quot;background:#1e1e2e; color:#cdd6f4; padding:20px 24px; border-radius:8px; font-size:14px; line-height:1.7; overflow-x:auto; margin-bottom:24px;&quot;&gt;&lt;code&gt;&lt;span style=&quot;color:#89b4fa;&quot;&gt;import&lt;/span&gt; pandas &lt;span style=&quot;color:#89b4fa;&quot;&gt;as&lt;/span&gt; pd

s = pd.Series([2, 4, 4, 4, 5, 5, 7, 9])
s.std()
&lt;span style=&quot;color:#6c7086;&quot;&gt;# 2.0&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

  &lt;br/&gt;

  &lt;h2 style=&quot;font-size: 22px; font-weight: 700; color: #1a1a1a; margin-top: 40px; margin-bottom: 16px;&quot;&gt;예시 2 — DataFrame (axis=0, 기본)&lt;/h2&gt;

  &lt;pre style=&quot;background:#1e1e2e; color:#cdd6f4; padding:20px 24px; border-radius:8px; font-size:14px; line-height:1.7; overflow-x:auto; margin-bottom:24px;&quot;&gt;&lt;code&gt;&lt;span style=&quot;color:#89b4fa;&quot;&gt;import&lt;/span&gt; pandas &lt;span style=&quot;color:#89b4fa;&quot;&gt;as&lt;/span&gt; pd

df = pd.DataFrame({
    &lt;span style=&quot;color:#a6e3a1;&quot;&gt;'국어'&lt;/span&gt;: [80, 90, 70, 100],
    &lt;span style=&quot;color:#a6e3a1;&quot;&gt;'수학'&lt;/span&gt;: [60, 95, 55, 90],
})

df.std()
&lt;span style=&quot;color:#6c7086;&quot;&gt;# 국어    12.91&lt;/span&gt;
&lt;span style=&quot;color:#6c7086;&quot;&gt;# 수학    19.76&lt;/span&gt;
&lt;span style=&quot;color:#6c7086;&quot;&gt;# 수학이 국어보다 점수 편차가 크다는 뜻&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

  &lt;br/&gt;

  &lt;h2 style=&quot;font-size: 22px; font-weight: 700; color: #1a1a1a; margin-top: 40px; margin-bottom: 16px;&quot;&gt;ddof=1 vs ddof=0&lt;/h2&gt;

  &lt;p style=&quot;margin: 0 0 16px;&quot;&gt;표준편차를 계산할 때 분모가 달라집니다.&lt;/p&gt;

  &lt;pre style=&quot;background:#1e1e2e; color:#cdd6f4; padding:20px 24px; border-radius:8px; font-size:14px; line-height:1.7; overflow-x:auto; margin-bottom:16px;&quot;&gt;&lt;code&gt;ddof=1  →  분모: n - 1   (표본 표준편차, pandas 기본값)
ddof=0  →  분모: n       (모 표준편차, numpy 기본값)&lt;/code&gt;&lt;/pre&gt;

  &lt;pre style=&quot;background:#1e1e2e; color:#cdd6f4; padding:20px 24px; border-radius:8px; font-size:14px; line-height:1.7; overflow-x:auto; margin-bottom:16px;&quot;&gt;&lt;code&gt;s = pd.Series([2, 4, 4, 4, 5, 5, 7, 9])

s.std(ddof=1)  &lt;span style=&quot;color:#6c7086;&quot;&gt;# 2.0   ← 표본, 일반적으로 이걸 씀&lt;/span&gt;
s.std(ddof=0)  &lt;span style=&quot;color:#6c7086;&quot;&gt;# 1.87  ← 모집단 전체를 갖고 있을 때&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

  &lt;div style=&quot;background:#fffbeb; border-left:4px solid #f59e0b; padding:14px 18px; border-radius:0 8px 8px 0; margin-bottom:24px; font-size:15px;&quot;&gt;
    표본(설문 응답자, 실험 측정값 등 일부 데이터)을 다룰 때는 &lt;code style=&quot;background:#eef2ff; color:#4f46e5; padding:2px 6px; border-radius:4px; font-size:90%;&quot;&gt;ddof=1&lt;/code&gt;, 모집단 전체 데이터를 갖고 있을 때는 &lt;code style=&quot;background:#eef2ff; color:#4f46e5; padding:2px 6px; border-radius:4px; font-size:90%;&quot;&gt;ddof=0&lt;/code&gt;을 씁니다.
  &lt;/div&gt;

  &lt;br/&gt;

  &lt;h2 style=&quot;font-size: 22px; font-weight: 700; color: #1a1a1a; margin-top: 40px; margin-bottom: 16px;&quot;&gt;실무 활용 — 이상치 탐지&lt;/h2&gt;

  &lt;p style=&quot;margin: 0 0 16px;&quot;&gt;정규분포에서 표준편차 범위별 포함 비율은 다음과 같습니다.&lt;/p&gt;

  &lt;pre style=&quot;background:#1e1e2e; color:#cdd6f4; padding:20px 24px; border-radius:8px; font-size:14px; line-height:1.7; overflow-x:auto; margin-bottom:16px;&quot;&gt;&lt;code&gt;±1σ  →  약 68.27%  포함  →  약 31.73% 가 바깥
±2σ  →  약 95.45%  포함  →  약  4.55% 가 바깥
±3σ  →  약 99.73%  포함  →  약  0.27% 가 바깥&lt;/code&gt;&lt;/pre&gt;

  &lt;p style=&quot;margin: 0 0 16px;&quot;&gt;평균 ± 2σ를 벗어나면 전체의 약 4.55%에 해당하는 극단값, ± 3σ를 벗어나면 약 0.27%에 해당하는 극단값입니다.&lt;/p&gt;

  &lt;pre style=&quot;background:#1e1e2e; color:#cdd6f4; padding:20px 24px; border-radius:8px; font-size:14px; line-height:1.7; overflow-x:auto; margin-bottom:16px;&quot;&gt;&lt;code&gt;mean = df[&lt;span style=&quot;color:#a6e3a1;&quot;&gt;'매출'&lt;/span&gt;].mean()
std  = df[&lt;span style=&quot;color:#a6e3a1;&quot;&gt;'매출'&lt;/span&gt;].std()

&lt;span style=&quot;color:#6c7086;&quot;&gt;# ±2σ 기준 — 조금 더 공격적으로 outlier를 잡음&lt;/span&gt;
outliers = df[(df[&lt;span style=&quot;color:#a6e3a1;&quot;&gt;'매출'&lt;/span&gt;] &amp;lt; mean - 2*std) | (df[&lt;span style=&quot;color:#a6e3a1;&quot;&gt;'매출'&lt;/span&gt;] &amp;gt; mean + 2*std)]

&lt;span style=&quot;color:#6c7086;&quot;&gt;# ±3σ 기준 — 정말 극단적인 값만 잡음&lt;/span&gt;
outliers = df[(df[&lt;span style=&quot;color:#a6e3a1;&quot;&gt;'매출'&lt;/span&gt;] &amp;lt; mean - 3*std) | (df[&lt;span style=&quot;color:#a6e3a1;&quot;&gt;'매출'&lt;/span&gt;] &amp;gt; mean + 3*std)]&lt;/code&gt;&lt;/pre&gt;

  &lt;div style=&quot;background:#fffbeb; border-left:4px solid #f59e0b; padding:14px 18px; border-radius:0 8px 8px 0; margin-bottom:24px; font-size:15px;&quot;&gt;
    실무에서는 &lt;code style=&quot;background:#eef2ff; color:#4f46e5; padding:2px 6px; border-radius:4px; font-size:90%;&quot;&gt;±2σ&lt;/code&gt; 또는 &lt;code style=&quot;background:#eef2ff; color:#4f46e5; padding:2px 6px; border-radius:4px; font-size:90%;&quot;&gt;±3σ&lt;/code&gt; 기준을 가장 많이 씁니다.
  &lt;/div&gt;

  &lt;br/&gt;

  &lt;h2 style=&quot;font-size: 22px; font-weight: 700; color: #1a1a1a; margin-top: 40px; margin-bottom: 16px;&quot;&gt;pandas vs numpy — ddof 기본값이 다르다&lt;/h2&gt;

  &lt;p style=&quot;margin: 0 0 16px;&quot;&gt;numpy에도 &lt;code style=&quot;background:#eef2ff; color:#4f46e5; padding:2px 8px; border-radius:4px; font-size:90%;&quot;&gt;np.std()&lt;/code&gt;가 있지만, &lt;strong&gt;ddof 기본값이 달라서&lt;/strong&gt; 같은 데이터에 아무 설정 없이 쓰면 결과가 다르게 나옵니다.&lt;/p&gt;

  &lt;pre style=&quot;background:#1e1e2e; color:#cdd6f4; padding:20px 24px; border-radius:8px; font-size:14px; line-height:1.7; overflow-x:auto; margin-bottom:16px;&quot;&gt;&lt;code&gt;&lt;span style=&quot;color:#89b4fa;&quot;&gt;import&lt;/span&gt; numpy &lt;span style=&quot;color:#89b4fa;&quot;&gt;as&lt;/span&gt; np
&lt;span style=&quot;color:#89b4fa;&quot;&gt;import&lt;/span&gt; pandas &lt;span style=&quot;color:#89b4fa;&quot;&gt;as&lt;/span&gt; pd

s   = pd.Series([2, 4, 4, 4, 5, 5, 7, 9])
arr = np.array([2, 4, 4, 4, 5, 5, 7, 9])

s.std()         &lt;span style=&quot;color:#6c7086;&quot;&gt;# 2.0   ← ddof=1 기본 (표본 표준편차)&lt;/span&gt;
np.std(arr)     &lt;span style=&quot;color:#6c7086;&quot;&gt;# 1.87  ← ddof=0 기본 (모 표준편차)&lt;/span&gt;

&lt;span style=&quot;color:#6c7086;&quot;&gt;# ddof를 맞춰주면 결과가 같아짐&lt;/span&gt;
np.std(arr, ddof=1)  &lt;span style=&quot;color:#6c7086;&quot;&gt;# 2.0  ← pandas와 동일&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

  &lt;table style=&quot;width:100%; border-collapse:collapse; margin-bottom:24px; font-size:15px;&quot;&gt;
    &lt;thead&gt;
      &lt;tr style=&quot;background:#4f8ef7; color:#fff;&quot;&gt;
        &lt;th style=&quot;padding:10px 16px; text-align:left; border:1px solid #ddd;&quot;&gt;&lt;/th&gt;
        &lt;th style=&quot;padding:10px 16px; text-align:left; border:1px solid #ddd;&quot;&gt;&lt;code style=&quot;background:rgba(255,255,255,0.2); padding:2px 6px; border-radius:4px; font-size:90%;&quot;&gt;pandas .std()&lt;/code&gt;&lt;/th&gt;
        &lt;th style=&quot;padding:10px 16px; text-align:left; border:1px solid #ddd;&quot;&gt;&lt;code style=&quot;background:rgba(255,255,255,0.2); padding:2px 6px; border-radius:4px; font-size:90%;&quot;&gt;numpy np.std()&lt;/code&gt;&lt;/th&gt;
      &lt;/tr&gt;
    &lt;/thead&gt;
    &lt;tbody&gt;
      &lt;tr style=&quot;background:#f9fafb;&quot;&gt;
        &lt;td style=&quot;padding:10px 16px; border:1px solid #ddd;&quot;&gt;ddof 기본값&lt;/td&gt;
        &lt;td style=&quot;padding:10px 16px; border:1px solid #ddd;&quot;&gt;1 (표본)&lt;/td&gt;
        &lt;td style=&quot;padding:10px 16px; border:1px solid #ddd;&quot;&gt;0 (모집단)&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
        &lt;td style=&quot;padding:10px 16px; border:1px solid #ddd;&quot;&gt;대상&lt;/td&gt;
        &lt;td style=&quot;padding:10px 16px; border:1px solid #ddd;&quot;&gt;Series, DataFrame&lt;/td&gt;
        &lt;td style=&quot;padding:10px 16px; border:1px solid #ddd;&quot;&gt;ndarray&lt;/td&gt;
      &lt;/tr&gt;
    &lt;/tbody&gt;
  &lt;/table&gt;

&lt;/div&gt;</description>
      <category>Python/Python Pandas</category>
      <category>PANDAS</category>
      <category>Python</category>
      <category>std</category>
      <category>표준편차</category>
      <author>CosmosProject</author>
      <guid isPermaLink="true">https://cosmosproject.tistory.com/934</guid>
      <comments>https://cosmosproject.tistory.com/934#entry934comment</comments>
      <pubDate>Thu, 14 May 2026 19:47:13 +0900</pubDate>
    </item>
  </channel>
</rss>