How to modify/update column values on a condition in Pydatatable?

巧了我就是萌 提交于 2021-02-08 04:15:53

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!