pandas change row value an existing column with conditionals - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Data Science (https://python-forum.io/forum-44.html) +--- Thread: pandas change row value an existing column with conditionals (/thread-19307.html) |
pandas change row value an existing column with conditionals - Gigux - Jun-22-2019 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-copyIs there a simple way to modify the rows of a column based on the values of another? Thank you. RE: pandas change row value an existing column with conditionals - Gigux - Jun-22-2019 Sorry, by running on another machine, it worked as it should have been. I simply used Y.loc[Y.agrmt == 0, 'cons'] = -1
|