Este producto no es compatible con el sitio Datadog seleccionado. ().
Esta página aún no está disponible en español. Estamos trabajando en su traducción.
Si tienes alguna pregunta o comentario sobre nuestro actual proyecto de traducción, no dudes en ponerte en contacto con nosotros.

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

Integraciones sin problemas. Prueba Datadog Code Security