问题
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?
回答1:
This should work:
py_DT[f.crossing == 'ABC', f.crossing] = 'A'
回答2:
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")
来源:https://stackoverflow.com/questions/59543028/how-to-modify-update-column-values-on-a-condition-in-pydatatable