How to swap row values in the same column of a data frame?

I have a data frame that looks like the following:

ID   Loc
 1    N  
 2    A   
 3    N
 4    H
 5    H  

I would like to swap A and H in the column Loc while not touching rows that have values of N, such that I get:

ID   Loc
 1    N  
 2    H   
 3    N
 4    A
 5    A 

This dataframe is the result of a pipe so I'm looking to see if it's possible to append this operation to the pipe.

We can try chaining together two calls to ifelse, for a base R option:

df <- data.frame(ID=c(1:5), Loc=c("N", "A", "N", "H", "H"), stringsAsFactors=FALSE)
df$Loc <- ifelse(df$Loc=="A", "H", ifelse(df$Loc=="H", "A", df$Loc))
df

  ID Loc
1  1   N
2  2   H
3  3   N
4  4   A
5  5   A

pandas: Transpose DataFrame (swap rows and columns), Use the T attribute or the transpose() method to swap (= transpose) the rows and columns of pandas. DataFrame . Neither method changes the original object, but returns a new object with the rows and columns swapped (= transposed object). Is it possible to restrict a data frame to a specific row and then change some values in one of the columns? Let's say I calculate GROWTH as (SIZE_t+1 - SIZE_t)/SIZE_t and now I can see that there are some strange values for GROWTH (e.g. 1000) and the reason is a corrupt value of the corresponding SIZE variable.

You could try:

df$Loc <- chartr("AH", "HA", df$Loc)
df

  ID Loc
1  1   N
2  2   H
3  3   N
4  4   A
5  5   A

Extract or Replace Parts of a Data Frame, If [ returns a data frame it will have unique (and non-missing) row names, row after the current last row, and the columns will be in-filled with missing values. 1] # a (unnamed) vector sw[[1]] # the same sw[1,] # a one-row data frame sw[1,,� The next task is to swap those values. If you are a developer and have learned the programming language in the class, you would think that you will need a third variable or a temporary storage location to swap the third value. However, if you are a SQL Server user, you can simply swap them using a single update statement. SWAP Column

If you have a factor, you could simply reverse those levels

l <- levels(df$Loc)
l[l %in% c("A", "N")] <- c("N", "A")

df
#   ID Loc
# 1  1   A
# 2  2   N
# 3  3   A
# 4  4   H
# 5  5   H

Data:

df <- structure(list(ID = 1:5, Loc = structure(c(3L, 1L, 3L, 2L, 2L
), .Label = c("A", "H", "N"), class = "factor")), .Names = c("ID", 
"Loc"), class = "data.frame", row.names = c(NA, -5L))

Pandas Tutorial: Replacing Values in , Matplotlib Tutorial: replace, at, loc to change values. We will work with the following DataFrame structure in our examples: loc on the other hand can be used to access a single value but also to access a group of rows and columns by a label or First, if to_replace and value are both lists, they must be the same length. Data Frame before Dropping Columns-Data Frame after Dropping Columns-For more examples refer to Delete columns from DataFrame using Pandas.drop() Dealing with Rows: In order to deal with rows, we can perform basic operations on rows like selecting, deleting, adding and renmaing. Row Selection: Pandas provide a unique method to retrieve rows

Frequently Asked Questions about data.table, 1.2 Why does DT[,"region"] return a 1-column data.table rather than a 2.5 Is a null data.table the same as DT[0] ? 2.11 Only the first 10 rows are printed, how do I print more? 2.17 What are the smaller syntax differences between data. frame and SD ”; 4.6 “cannot change value of locked binding for . Use the T attribute or the transpose () method to swap (= transpose) the rows and columns of pandas.DataFrame. Neither method changes the original object, but returns a new object with the rows and columns swapped (= transposed object).

Dataframe rearrangement, reorder, reshape, or otherwise change the organization of a dataframe. Sometimes we want to get dataframe columns in a different order from how they' re read into the data. Changing row order works the same way as changing column order. Instead we might want to reorder according to the values of a column. Now, if we want to change the row indexes and column names simultaneously, then it can be achieved using rename() function and passing both column and index attribute as the parameter. filter_none edit

Indexing and selecting data — pandas 0.8.1 documentation, You may select rows from a DataFrame using a boolean vector the same This allows you to select rows where one or more columns have values you want: when the API change was made to stop “falling back” on position-based indexing ). t() for transpose. But note that the column names become row names, too, so you may need to think about appropriate column names for the transposed data structure: > d &lt;- data.frame(hero=c(&quot;Superman&quot;, &quot;Batman&quot;, &quot;Vijay&quot;), score=c(100, 100, -7)) &gt;