이 제품은 선택한 Datadog 사이트에서 지원되지 않습니다. ().
이 페이지는 아직 한국어로 제공되지 않습니다. 번역 작업 중입니다.
현재 번역 프로젝트에 대한 질문이나 피드백이 있으신 경우 언제든지 연락주시기 바랍니다.

Metadata

ID: python-pandas/pivot-table

Language: Python

Severity: Notice

Category: Best Practices

Description

This rule encourages the use of pivot_table instead of pivot or unstack for reshaping data in pandas DataFrames. While pivot and unstack can be simpler for straightforward cases, they are limited because they do not handle duplicate values well and can raise errors if the data is not perfectly formatted.

Using pivot_table is important because it provides greater flexibility by allowing aggregation functions to handle duplicates and missing values gracefully. This makes your code more robust, especially when working with real-world data that often contains duplicates or incomplete entries.

Non-Compliant Code Examples

table = df.unstack(level=0)
table = pd.pivot(
        df,
        index='foo',
        columns='bar',
        values='baz'
        )

Compliant Code Examples

table = df.pivot_table(
        df,
        values='D',
        index=['A', 'B'],
        columns=['C'],
        aggfunc=np.sum,
        fill_value=0
https://static.datadoghq.com/static/images/logos/github_avatar.svg https://static.datadoghq.com/static/images/logos/vscode_avatar.svg jetbrains

원활한 통합. Datadog Code Security를 경험해 보세요