Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Merging two data frames into a new one with unique items marked with 1 or 0

I have several dataframes.

Dataframe #1

Feature   Coeff
a         0.5
b         0.3
c         0.35
d         0.2

Dataframe #2

Feature   Coeff
a         0.7
b         0.2
y         0.75
x         0.1

I want to merge this dataframe and obtain the following one:

Feature |  DF1  |  DF2
a          1       1
b          1       1
c          1       0
d          1       0
y          0       1
x          0       1

I know that I can do an outer merge but I do not know how to move from there to obtain the final dataframe I presented above. Any ideas?

like image 363
renakre Avatar asked Jun 30 '19 16:06

renakre


People also ask

How do I merge two data frames?

We can use the concat function in pandas to append either columns or rows from one DataFrame to another. Let's grab two subsets of our data to see how this works. When we concatenate DataFrames, we need to specify the axis. axis=0 tells pandas to stack the second DataFrame UNDER the first one.

Which function is used to merge two data frames?

The concat() function can be used to concatenate two Dataframes by adding the rows of one to the other.

How do I merge unique values in pandas?

merge() method joins two data frames by a “key” variable that contains unique values. With pandas. merge(), you can only combine 2 data frames at a time. If you have more than 2 data frames to merge, you will have to use this method multiple times.

How do I get unique values from two data frames?

To get the unique values in multiple columns of a dataframe, we can merge the contents of those columns to create a single series object and then can call unique() function on that series object i.e. It returns the count of unique elements in multiple columns.


1 Answers

Using concat+ get_dummies

u = pd.concat([df1, df2], axis=0, keys=['DF1', 'DF2'])

pd.get_dummies(u.Feature).sum(level=0).T

   DF1  DF2
a    1    1
b    1    1
c    1    0
d    1    0
x    0    1
y    0    1
like image 71
user3483203 Avatar answered Sep 22 '22 07:09

user3483203