How to update column x at row where column y = z[QUESTION]
gagecarto opened this issue · comments
Ask your question
I am having a hard time figuring out how to update a particular cell value by first selecting a row.
Let's say I have a dataframe with columns x,y & z
I'd like to update column x to a value of 999 at the row where column y == 5
I know I can select the appropriate row by doing
df.filter(row => row.get('y') === 5)
I tried chaining this to a set call but was obviously first subsetting the entire dataframe.
df= df.chain(
row => row.get('y') == 5, // filter
row => row.set('x', 999) // map
)
Any suggestions?
Additional context
Add any other context or screenshots about the question here.
I was able to figure this out using a combination of prerolled row indices and set row function.. It worked like this
rowIndices=myDf.toDict()['y']
function updateDf(rowValue,newValue){
var tempIndex=rowIndices.indexOf(rowValue)
myDf=myDf.setRow(tempIndex, row => row.set('x',999))
}
// i can now update column x to 999 where row y == 5 using function call like below
updateDf(5,999)