How do i replace values from a single column of a dataframe. For example all 0 values in column dataz to values of 1
datay dataz
[1,] 0 100
[2,] 2 101
[3,] 3 102
[4,] 4 103
[5,] 10 0
[6,] 11 0
[7,] 0 0
[8,] 0 0
[9,] 0 0
[10,] 12 11
[11,] 45 12
Re: Restricting Find/Replace to single column Or if you want to replace values in a selected range, select all the cells where you want to replace a value and then invoke the replace window by pressing Ctrl+H.
Pandas DataFrame replace() Method The replace() method replaces the specified value with another specified value. The replace() method searches the entire DataFrame and replaces every case of the specified value.
Suppose that you want to replace multiple values with multiple new values for an individual DataFrame column. In that case, you may use this template: df['column name'] = df['column name']. replace(['1st old value', '2nd old value', ...], ['1st new value', '2nd new value', ...])
In order to replace a value in Pandas DataFrame, use the replace() method with the column the from and to values. Below example replace Spark with PySpark value on the Course column. Notice that all the Spark values are replaced with the Pyspark values under the first column.
This will change values of 5 to NA. You can also use a range.
df <- data.frame(datay = sample(1:5, 10, replace = TRUE), dataz = sample(letters, 10, replace = TRUE))
df$datay[df$datay == 5] <- NA
This will find data smaller than 3 and bigger than 1. Use assign (<-
) to assign your value.
df$datay[df$datay < 3 & df$datay > 1]
Here's a quick example of ifelse
.
ifelse(df$dataz == "w", "oi!", NA)
Load your data by (for others to easily do that for your reproducible example):
> text <- textConnection(" datay dataz
+ [1,] 0 100
+ [2,] 2 101
+ [3,] 3 102
+ [4,] 4 103
+ [5,] 10 0
+ [6,] 11 0
+ [7,] 0 0
+ [8,] 0 0
+ [9,] 0 0
+ [10,] 12 11
+ [11,] 45 12")
> df <- read.table(text, header=TRUE)
Or by using the output of dput
applied to your data frame:
> df <- structure(list(datay = c(0L, 2L, 3L, 4L, 10L, 11L, 0L, 0L, 0L,
12L, 45L), dataz = c(100L, 101L, 102L, 103L, 0L, 0L, 0L, 0L,
0L, 11L, 12L)), .Names = c("datay", "dataz"), class = "data.frame", row.names = c("[1,]",
"[2,]", "[3,]", "[4,]", "[5,]", "[6,]", "[7,]", "[8,]", "[9,]",
"[10,]", "[11,]"))
To change "all 0 values in column dataz to values of 1":
> df$dataz[df$dataz == 0] <- 1
> df
datay dataz
[1,] 0 100
[2,] 2 101
[3,] 3 102
[4,] 4 103
[5,] 10 1
[6,] 11 1
[7,] 0 1
[8,] 0 1
[9,] 0 1
[10,] 12 11
[11,] 45 12
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With