I have some issue in my code.
I tried to create new columns by comparing existing two columns
Here is my example Dataframe
occ_2019_pe:
index occ10 occ10_t1
0 20 20
1 20 12
2 30 30
3 40 34
I want to generate new column by using this code
occ_2019_pe['occ_change'] = np.where(occ_2019_pe['occ10']==occ_2019_pe['occ10_t1'], 0, 1)
However, it generate this issue,
<ipython-input-25-f48cdb1733d5>:1: 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
occ_2019_pe['occ_change'] = np.where(occ_2019_pe['occ10']==occ_2019_pe['occ10_t1'], 0, 1)
How could I solve this?
CodePudding user response:
Use assign.
occ_2019_pe = occ_2019_pe.assign(occ_change=np.where(occ_2019_pe['occ10']==occ_2019_pe['occ10_t1'], 0, 1))
CodePudding user response:
When I run code with your small example then I don't get this error.
But maybe you should use suggested .loc[row_indexer,col_indexer] = value
occ_2019_pe.loc[:,'occ_change'] = np.where(occ_2019_pe['occ10']==occ_2019_pe['occ10_t1'], 0, 1)