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?
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.
The concat() function can be used to concatenate two Dataframes by adding the rows of one to the other.
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.
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.
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
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