Sep-07-2023, 04:16 AM
In another thread I posted this code:
import pandas as pd
from string import ascii_letters as letters
from random import choice, choices, randint
def find_supplier(description):
"""Return word if word in description matches a supplier code, else None."""
intersection = set(description.split()) & suppliers
return list(intersection)[0] if intersection else None
# Make some random table thing that we can use to search for words in the description
# that match a supplier code.
product_table = pd.DataFrame(
[
{
"Product": i,
"Supplier Code": choice("ABCDE"),
"Description": " ".join(choices(letters, k=randint(5, 10))),
}
for i in range(100, 120)
]
)
# Get set of suppliers.
suppliers = set(product_table["Supplier Code"].values)
# Make supplier table. Supplier table contains rows from product_table
# where one of the words in the description matches a supplier code.
supplier_table = product_table[["Description"]]
supplier_table["Product"] = supplier_table["Description"].map(find_supplier)
supplier_table = supplier_table[~supplier_table["Product"].isna()][
["Product", "Description"]
]
print(supplier_table)When I run it I get a warning.Error:...test.py:31: SettingWithCopyWarning:
A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead
See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy
supplier_table["Product"] = product_table["Description"].apply(find_supplier)I've seen this messge before. In other cases the chaining was obvious and easy to fix. Here I cannot see the chaining and I have no idea how to fix.
