Python Forum
Fix pandas copy/slice warning.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Fix pandas copy/slice warning.
#3
Try change to this.
SettingWithCopyWarning is generated by pandas when you are modifying a DataFrame that is actually a view of another DataFrame
By using .copy() to make a copy of the DataFrame before modifying it,should avoid the SettingWithCopyWarning.
This ensures that are working on a new DataFrame rather than inadvertently modifying the original one.
# 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"]].copy()
supplier_table["Product"] = supplier_table["Description"].map(find_supplier)
supplier_table = supplier_table[~supplier_table["Product"].isna()]
supplier_table = supplier_table[["Product", "Description"]]
print(supplier_table)
Reply


Messages In This Thread
Fix pandas copy/slice warning. - by deanhystad - Sep-07-2023, 04:16 AM
RE: Fix pandas copy/slice warning. - by Pedroski55 - Sep-07-2023, 06:28 AM
RE: Fix pandas copy/slice warning. - by snippsat - Sep-07-2023, 07:12 AM
RE: Fix pandas copy/slice warning. - by deanhystad - Sep-07-2023, 03:18 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Why is the copy method name in python list copy and not `__copy__`? YouHoGeon 2 388 Apr-04-2024, 01:18 AM
Last Post: YouHoGeon
  Dataframe copy warning sawtooth500 4 502 Mar-25-2024, 11:38 PM
Last Post: sawtooth500
  Slice creates new objects? fmr300 4 1,429 Jul-20-2022, 12:34 PM
Last Post: fmr300
  InvalidIndexError: (slice(None, None, None), slice(None, -1, None)) SuperNinja3I3 1 4,687 Jul-15-2022, 05:59 AM
Last Post: Larz60+
  Slice list Moris526 1 1,714 Dec-24-2020, 02:19 AM
Last Post: deanhystad
  increase and decrease a slice value? KEYS 2 2,174 Nov-10-2020, 11:35 PM
Last Post: KEYS
  Pass Tuple as a Slice nagymusic 2 2,439 Dec-12-2019, 04:42 AM
Last Post: nagymusic
  Preferred way to slice a list SvetlanaofVodianova 3 2,638 Dec-09-2019, 11:50 PM
Last Post: SvetlanaofVodianova
  slice python array on condition Gigux 2 2,340 Nov-03-2019, 07:21 PM
Last Post: Larz60+
  How to append and drop to next line while slice/indexing emryscass 3 2,720 Sep-26-2019, 01:06 PM
Last Post: Malt

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020