Skip to content

DOC: Pandas Case_When  #57245

@MesumRaza

Description

@MesumRaza

Pandas version checks

  • I have checked that the issue still exists on the latest versions of the docs on main here

Location of the documentation

https://pandas.pydata.org/docs/dev/reference/api/pandas.Series.case_when.html

Documentation problem

The example provide is not intuitive.

Suggested fix for documentation

I found the example of pd.Series.case_when very non intuitive hence here is the updated example

Examples
--------
>>> # Real-world example: Updating product prices based on a dynamic pricing strategy
>>> current_prices = pd.Series([50, 75, 100, 120], name='current_prices')
>>> demand_forecast = pd.Series([100, 80, 50, 120], name='demand_forecast')

    >>> # Conditions for price adjustments based on demand forecast
    >>> conditions = [
    ...     (demand_forecast > 90, current_prices * 1.2),  # Increase by 20% for high demand
    ...     (demand_forecast < 60, current_prices * 0.8),  # Decrease by 20% for low demand
    ...     (current_prices > 100, current_prices * 0.95)   # Apply a 5% discount for high-priced items
    ... ]

    >>> # Applying the dynamic pricing strategy
    >>> updated_prices = current_prices.case_when(caselist=conditions)
    >>> updated_prices
    0     60.0
    1     75.0
    2     80.0
    3    114.0
    Name: current_prices, dtype: float64

Metadata

Metadata

Assignees

No one assigned

    Labels

    ConditionalsE.g. where, mask, case_whenDocsNeeds InfoClarification about behavior needed to assess issue

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions