Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding intersection of dataframe rownames

I have two dataframes. The row names in both are dates. What I want to do is, I want to select all the common rows (having same dates) in both the data frames and create a new data frame having only these common rows.

Of course the individual columns would get appended next to each other.

Can anyone please help??

like image 559
user2127116 Avatar asked Mar 18 '13 21:03

user2127116


People also ask

How do you find the intersection of a data frame?

Intersection of Two data frames in Pandas can be easily calculated by using the pre-defined function merge() . This function takes both the data frames as argument and returns the intersection between them.

How do you find common rows in two DataFrames?

To find the common rows between two DataFrames with merge(), use the parameter “how” as “inner” since it works like SQL Inner Join and this is what we want to achieve.


1 Answers

Try:

merge(df1, df2, by="row.names")
?merge

Can also use by=0 instead of 'row.names'. And BTW the rownames are not R Date class, but are character valued. I suppose one could also do this:

 cbind( df1[ intersect(rownames(df1), rownames(df2)), ] ,
        df2[ intersect(rownames(df1), rownames(df2)), ] )
like image 104
IRTFM Avatar answered Sep 21 '22 16:09

IRTFM