Posts: 38
Threads: 16
Joined: Jan 2020
Why am I getting the "setting with copy" warning with the code below? Am I not doing what they suggested? And the result is wrong so I guess there is some problem with this.
import pandas as pd
'''
...
'''
#dfout is a pd.DataFrame instance
dfout.columns=[0,1,2]
dfout.loc[:,3]=dfout.loc[:,0]
dfout.loc[:,4]=dfout.loc[:,1]
dfout.loc[:,5]=dfout.loc[:,2]
dfout.loc[:,6]=np.sqrt(dfout.loc[:,4]**2+dfout.loc[:,5]**2)
dfout.loc[:,7]=np.arctan(dfout.loc[:,5]/dfout.loc[:,4])/np.pi*360 Thanks
Posts: 38
Threads: 16
Joined: Jan 2020
(Feb-13-2023, 02:07 PM)catlessness Wrote: Why am I getting the "setting with copy" warning with the code below? Am I not doing what they suggested? And the result is wrong so I guess there is some problem with this.
import pandas as pd
'''
...
'''
#dfout is a pd.DataFrame instance
dfout.columns=[0,1,2]
dfout.loc[:,3]=dfout.loc[:,0]
dfout.loc[:,4]=dfout.loc[:,1]
dfout.loc[:,5]=dfout.loc[:,2]
dfout.loc[:,6]=np.sqrt(dfout.loc[:,4]**2+dfout.loc[:,5]**2)
dfout.loc[:,7]=np.arctan(dfout.loc[:,5]/dfout.loc[:,4])/np.pi*360 Thanks Oh the result is right I just checked again
Posts: 16
Threads: 3
Joined: Jan 2023
If you are looking for help with a specific problem, it is best to post a new thread with a detailed description of your issue. This will help other members of the community provide you with the best advice and assistance.
Posts: 16
Threads: 3
Joined: Jan 2023
You are getting the "setting with copy" warning because you are trying to assign a value to a subset of a DataFrame without using the .loc accessor. The .loc accessor is used to ensure that the original DataFrame is not modified in place.
To fix this, you should use the .loc accessor when assigning values to a subset of a DataFrame:
dfout.loc[:,3] = dfout.loc[:,0]
dfout.loc[:,4] = dfout.loc[:,1]
dfout.loc[:,5] = dfout.loc[:,2]
dfout.loc[:,6] = np.sqrt(dfout.loc[:,4]**2+dfout.loc[:,5]**2)
dfout.loc[:,7] = np.arctan(dfout.loc[:,5]/dfout.loc[:,4])/np.pi*360
Posts: 38
Threads: 16
Joined: Jan 2020
(Feb-14-2023, 09:59 AM)Vadanane Wrote: You are getting the "setting with copy" warning because you are trying to assign a value to a subset of a DataFrame without using the .loc accessor. The .loc accessor is used to ensure that the original DataFrame is not modified in place.
To fix this, you should use the .loc accessor when assigning values to a subset of a DataFrame:
dfout.loc[:,3] = dfout.loc[:,0]
dfout.loc[:,4] = dfout.loc[:,1]
dfout.loc[:,5] = dfout.loc[:,2]
dfout.loc[:,6] = np.sqrt(dfout.loc[:,4]**2+dfout.loc[:,5]**2)
dfout.loc[:,7] = np.arctan(dfout.loc[:,5]/dfout.loc[:,4])/np.pi*360
Thank you, but isn't this exactly what I have done?
Posts: 6,120
Threads: 16
Joined: Feb 2020
The warning is triggered by a pattern, not an actual promlem. Sometimes you cannot do anything to stop the warning other than not modifying slices in the dataframe.
Why are you using slices instead of indicies?
|