How can you replace a column of a Dataset with another column or list?
The easiest way to do this, is probably by using Part
assignment. Unfortunately, though, that doesn't work on Dataset
s, so we have to convert back to Normal
form to do so. Here's an example:
Create example dataset:
length = 10;
dat = Dataset[
AssociationThread[{"x", "y"}, #] & /@ RandomInteger[{1, 100}, {length, 2}]
]
Create values to replace the x
column:
newcolumnX = Range[length]
Use Part
assignment to replace the column values; then convert back to Dataset
:
dat = Normal[dat];
dat[[All, "x"]] = newcolumnX;
dat = Dataset[dat]
Alternatively, you can Transpose
the dataset and then Append
/Prepend
a new column in one go (and then transpose back):
dat = Transpose[
Prepend[Transpose[dat], "x" -> newcolumnX]
]