When try to filter data and make some change as per requirement even afte make new Df from pandas

C:\Users\Vishal Sharma\Desktop\Python\2B\F 2B.py:946: SettingWithCopyWarning:
A value is trying to be set on a copy of a slice from a DataFrame

See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy
test.rename(columns = {a:"Last " +a},inplace = True)
C:\Users\Vishal Sharma\Desktop\Python\2B\F 2B.py:946: SettingWithCopyWarning:
A value is trying to be set on a copy of a slice from a DataFrame

# Code Start with
# Filter down complete data of overvall recon
test = Final_Recon_read[Final_Recon_read['Remark']== 'Matched']

for a in  test.columns.difference((['GSTIN of supplier', 'Trade/Legal name', 'Invoice number','Invoice Date', 'Supply Attract Reverse Charge'
    , 'Place of supply',  'Source','Purchase Voucher no', 'Voucher Type'])):
    test.rename(columns = {a:"Last " +a},inplace = True)
    

>Solution :

You are trying to assign the value of a filtered data frame to another variable and although pandas will generally allow it, it will raise an error.

Assign a copy of the data frame instead using .copy()

test = Final_Recon_read[Final_Recon_read['Remark']== 'Matched'].copy()

Leave a Reply