Python Forum

Full Version: pandas change row value an existing column with conditionals
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello,
I have loaded a dataframe in tsv format with pandas with:
import numpy as np
import pandas as pd
df =  pd.read_csv(<file>, delimiter='\t')
Now I would like to modify the rows of a column based on the condition of another column. This should be pretty simple but I can't find a clear syntax and I am keeping getting errors:

Y.loc[Y.agrmt == 0, 'cons'] = -1
~.local/lib/python3.6/site-packages/pandas/core/indexing.py:190: SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame

See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy
  self._setitem_with_indexer(indexer, value)
__main__:1: SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame

See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy

Y.loc[Y.agrmt == 0, ['cons']] = 0
~.local/lib/python3.6/site-packages/pandas/core/indexing.py:190: SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame

See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy
  self._setitem_with_indexer(indexer, value)
__main__:1: SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame

See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy
Is there a simple way to modify the rows of a column based on the values of another?
Thank you.
Sorry, by running on another machine, it worked as it should have been. I simply used Y.loc[Y.agrmt == 0, 'cons'] = -1