For example if I have this:
n = c(2, 3, 5) s = c("aa", "bb", "cc") b = c(TRUE, FALSE, TRUE) df = data.frame(n, s, b) n s b 1 2 aa TRUE 2 3 bb FALSE 3 5 cc TRUE
Then how do I combine the two columns n
and s
into a new column named x
such that it looks like this:
n s b x 1 2 aa TRUE 2 aa 2 3 bb FALSE 3 bb 3 5 cc TRUE 5 cc
Different column names are specified for merges in Pandas using the “left_on” and “right_on” parameters, instead of using only the “on” parameter. Merging dataframes with different names for the joining variable is achieved using the left_on and right_on arguments to the pandas merge function.
By use + operator simply you can concatenate two or multiple text/string columns in pandas DataFrame.
It is possible to join the different columns is using concat() method. DataFrame: It is dataframe name. axis: 0 refers to the row axis and1 refers the column axis. join: Type of join.
We can join columns from two Dataframes using the merge() function. This is similar to the SQL 'join' functionality. A detailed discussion of different join types is given in the SQL lesson.
Use paste
.
df$x <- paste(df$n,df$s) df # n s b x # 1 2 aa TRUE 2 aa # 2 3 bb FALSE 3 bb # 3 5 cc TRUE 5 cc
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