Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 pandas change row value an existing column with conditionals
#1
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.
Quote
#2
Sorry, by running on another machine, it worked as it should have been. I simply used Y.loc[Y.agrmt == 0, 'cons'] = -1
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Pandas - Dynamic column aggregation based on another column theroadbacktonature 0 156 Apr-17-2020, 04:54 PM
Last Post: theroadbacktonature
  Add column to CSV using Pandas nsadams87xx 2 260 Apr-15-2020, 08:41 PM
Last Post: snippsat
  add formatted column to pandas data frame alkaline3 0 182 Mar-22-2020, 06:44 PM
Last Post: alkaline3
  Code improvement: groupby and operation on conditionals pythonidae 6 525 Dec-12-2019, 05:58 AM
Last Post: pythonidae
  itertuples, new column, datetime, pandas karlito 6 597 Nov-29-2019, 11:07 AM
Last Post: karlito
  sort values of a column pandas karlito 2 534 Oct-22-2019, 06:11 AM
Last Post: karlito
  Dropping a column from pandas dataframe marco_ita 6 4,209 Sep-07-2019, 08:36 AM
Last Post: marco_ita
  How to drop column in pandas SriMekala 3 777 Aug-26-2019, 06:36 PM
Last Post: snippsat
  Pandas Import CSV count between numerical values within 1 Column ptaylor520 3 494 Jul-16-2019, 08:13 AM
Last Post: ptaylor520
  Change column names from a file Nidhesh 2 741 Jul-08-2019, 06:00 AM
Last Post: Nidhesh

Forum Jump:


Users browsing this thread: 1 Guest(s)