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
  sort values of a column pandas karlito 2 281 Oct-22-2019, 06:11 AM
Last Post: karlito
  Dropping a column from pandas dataframe marco_ita 6 761 Sep-07-2019, 08:36 AM
Last Post: marco_ita
  How to drop column in pandas SriMekala 3 466 Aug-26-2019, 06:36 PM
Last Post: snippsat
  Pandas Import CSV count between numerical values within 1 Column ptaylor520 3 245 Jul-16-2019, 08:13 AM
Last Post: ptaylor520
  Change column names from a file Nidhesh 2 375 Jul-08-2019, 06:00 AM
Last Post: Nidhesh
  How to use the excel filename as a value to populate new column, using Pandas? Dequanharrison 5 487 Jun-26-2019, 11:11 PM
Last Post: Dequanharrison
  How to delete column if entire column values are "nan" Sri 4 611 Apr-13-2019, 12:16 PM
Last Post: Sri
  pandas best way to add a new row in a already existing dataframe aster 0 566 Sep-17-2018, 02:25 PM
Last Post: aster
  Text to column pandas ms5573 0 604 Aug-25-2018, 08:18 PM
Last Post: ms5573
  Splitting values in column in a pandas dataframe based on a condition hey_arnold 1 1,540 Jul-24-2018, 02:18 PM
Last Post: hey_arnold

Forum Jump:


Users browsing this thread: 1 Guest(s)