Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Combine two data frames and remove duplicate columns

I want to cbind two data frames and remove duplicated columns. For example:

df1 <- data.frame(var1=c('a','b','c'), var2=c(1,2,3))
df2 <- data.frame(var1=c('a','b','c'), var3=c(2,4,6))

cbind(df1,df2) #this creates a data frame in which column var1 is duplicated

I want to create a data frame with columns var1, var2 and var3, in which column var2 is not repeated.

like image 772
danilinares Avatar asked Sep 16 '11 06:09

danilinares


People also ask

How do I remove duplicate columns from a data frame?

To drop duplicate columns from pandas DataFrame use df. T. drop_duplicates(). T , this removes all columns that have the same data regardless of column names.

How do I combine two DataFrame columns?

By use + operator simply you can concatenate two or multiple text/string columns in pandas DataFrame.

How do I combine multiple data frames into one?

merge() to merge multiple Dataframes. Merging multiple Dataframes is similar to SQL join and supports different types of join inner , left , right , outer , cross . In this article, we will learn how to merge multiple (three or more) Dataframes with examples. Yields below the output of three DataFrames.


2 Answers

merge will do that work.

try:

merge(df1, df2)
like image 164
kohske Avatar answered Sep 29 '22 15:09

kohske


In case you inherit someone else's dataset and end up with duplicate columns somehow and want to deal with them, this is a nice way to do it:

for (name in unique(names(testframe))) {
  if (length(which(names(testframe)==name)) > 1) {
    ## Deal with duplicates here. In this example
    ## just print name and column #s of duplicates:
    print(name)
    print(which(names(testframe)==name))
  }
}
like image 25
Sam Avatar answered Sep 29 '22 15:09

Sam