I am working with a data frame in which I need to edit the entries in one particular column to allow for easy sorting. The data frame looks like this when imported:
Assay Genotype Description Sample Operator
1 CCT6-18 C A.Conservative 1_062911 Automatic
2 CCT6-24 C E.User Call 1_062911 charles
3 CCT6-25 A A.Conservative 1_062911 Automatic
I need to change the assay column from CCT6-18 to CCT6-018. This "assay" appears multiple times within the data frame and I'd like to change all of the entries at once. Ive tried the gsub function but it returns data in a format that I am unfamiliar with. I'd like to get the data back in a data frame.
Help!
You can replace a string in the pandas DataFrame column by using replace(), str. replace() with lambda functions.
Pandas DataFrame replace() MethodThe 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.
To change a text value in an R data frame, we can simply use replace function.
Using Python replace() method, we can update or change the value of any string within a data frame. We need not provide the index or label values to it. As seen above, we have replaced the word “Siri” with “Code” within the dataframe.
df$Assay <- replace(df$Assay, df$Assay=="CCT6-18", "CCT6-018")
Should see you right.
Also, try str(df)
or class(df$Assay)
to see what class your Assay column is. If it is a factor this could be the reason you're getting tripped up. If so run df$Assay <- as.character(df$Assay)
first.
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