This product is not supported for your selected Datadog site. ().
このページは日本語には対応しておりません。随時翻訳に取り組んでいます。
翻訳に関してご質問やご意見ございましたら、お気軽にご連絡ください

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 をお試しください