ChatGPT for pandas: keep values attached to their index labels

Give ChatGPT both the DataFrame index and the incoming Series index when a pandas column receives the wrong values. Assigning a Series aligns its values by label. Converting that Series to an unlabelled array changes the operation to positional assignment, which can attach the same numbers to different records.

This often appears after sorting. The table still contains the expected number of rows, and a total can remain correct while individual records receive the wrong amounts. Check the value for each known record ID, not just the sum.

Show both orders before asking for a fix

This original fictional fixture lists O2 before O1, while the incoming price Series lists O1 before O2:

import pandas as pd

orders = pd.DataFrame(
    {"item": ["Binder", "Notebook"]},
    index=pd.Index(["O2", "O1"], name="order_id"),
)
prices = pd.Series([12, 18], index=["O1", "O2"], name="unit_price")

reviewed = orders.copy()
reviewed["unit_price"] = prices
assert reviewed.loc["O1", "unit_price"] == 12
assert reviewed.loc["O2", "unit_price"] == 18
assert reviewed.index.tolist() == ["O2", "O1"]
print(reviewed.to_string())

The expected displayed rows are O2 / Binder / 18 and O1 / Notebook / 12. The assignment follows the Series labels; it does not first reorder the DataFrame into O1, O2.

The official pandas indexing guide, checked October 24, 2026, explains label-based selection, positional selection and data alignment. In this example, .loc["O1"] selects the label O1. It does not mean the first row.

See what removing the labels changes

This alternative deliberately demonstrates the wrong operation for the fixture's label-based price contract:

positional = orders.copy()
positional["unit_price"] = prices.to_numpy()
assert positional.loc["O2", "unit_price"] == 12
assert positional.loc["O1", "unit_price"] == 18

The array contains 12 followed by 18, so those values are assigned to the first and second rows. Its labels are gone. The combined price remains 30 in both versions, making a total-only check unable to detect the swap.

Positional assignment can be intentional when the supplied array is explicitly in destination-row order. Here, the source says the prices belong to order labels, so keeping that identity is the appropriate rule. Do not reset indexes or sort both objects merely to make a small example look aligned; first establish what the labels represent.

Inspect missing and duplicate labels separately

If the price Series contains O1 but no O2, label-aligned assignment leaves O2's new value missing. That is evidence of an incomplete source under this task's rule. Filling it with zero would invent a price.

The fixture uses unique labels. Before adapting it, check whether both indexes are unique and whether each required destination ID has a supplied value. A duplicate order label requires an explicit identity rule; a model should not silently choose one occurrence or assume an undocumented row order.

Explain this pandas column assignment before changing it.
Python/pandas versions: [ENTER]
DataFrame construction or small permitted sample including its index: [PASTE]
Incoming Series construction including its index and dtype: [PASTE]
Exact assignment and observed result: [PASTE]
Meaning of each index and required record-to-value mapping: [ENTER]
Missing/duplicate-label policy: [ENTER]

Trace label-based alignment versus positional assignment. Show expected
values by record ID and preserve destination row order. Do not discard
labels, reset indexes, fill missing values or deduplicate by convention.
Propose the smallest correction and assertions for two distinct IDs.
Keep original inputs unchanged and distinguish expected output from
actual execution. Ask for missing identity rules before writing a patch.

The goal-and-context approach follows OpenAI's prompting guidance. Supplying only the visible two numeric columns would omit the labels that explain this behavior.

Run the small case in your pandas environment and inspect the ID-specific assertions. For questions about which records belong in a report's denominator, the data-analysis guide handles a different decision. Accept this assignment after each known ID receives its intended value and missing source IDs remain visible for review.

Keep reading