1

In pydatatable, I'm trying to modify a column values specifying a condition in i i.e DT[i=="text", j="some"]

sample DT:

py_DT= dt.Frame({'crossing':['ABC','A','B','B','A','A','ABC'],
                 'total' :[2,4,5,6,8,10,12]})

Here i would like to replace crossing value 'ABC' with 'A' only, for that i have written a below sample code,

Attempt 1:

py_DT[f.crossing=="ABC", f.crossing=="A"]

Attempt 2:

py_DT[f.crossing=="ABC", update(f.crossing=="A")]

None of these attempts were worked out, is there any other way to get it solved? Could you please write to me how to update a column value as per the said requirement?

Pasha
  • 6,298
  • 2
  • 22
  • 34
myamulla_ciencia
  • 1,282
  • 1
  • 8
  • 30

2 Answers2

3

This should work:

py_DT[f.crossing == 'ABC', f.crossing] = 'A'
josemz
  • 1,283
  • 7
  • 15
0

If I understand correctly you want to replace all 'ABC' values, right? In that case you could use df.str.replace():

py_DT['crossing'].str.replace("ABC", "A")
Sverre
  • 54
  • 4
  • thanks for your answer. that can be OK in pandas dataframe. But i wanted to do it in pydatatable way. – myamulla_ciencia Dec 31 '19 at 11:11
  • I've looked at the documentation for [datatable](https://datatable.readthedocs.io/en/latest/quick-start.html) and couldn't find anything other than Pandas to replace. I'd recommend just converting it to a Pandas dataframe like they do [here at the replace() part.](https://datatable.readthedocs.io/en/latest/api/frame.html) – Sverre Dec 31 '19 at 11:25
  • Yes, the pandas workaround can be worked out and i'm just awaiting for a response from pydatatable team members or other who could come across this situation earlier, lets wait and see. – myamulla_ciencia Dec 31 '19 at 11:56