Merging two data frames into a new one with unique items marked with 1 or 0How to merge two dictionaries in a single expression?How to join (merge) data frames (inner, outer, left, right)Drop data frame columns by name“Large data” work flows using pandasMerging two data frames based on row values in python pandasMerge two python pandas data frames of different length but keep all rows in output data frameGet unmerged data from source tablesMerging two data frames with flexible conditionsCompare and equalize two data frames in PythonJoin two data frame with two columns values of a df with a single column values of another dataframe. based on some conditions?
Odd PCB Layout for Voltage Regulator
Why is quantum gravity non-renormalizable?
Searching for single buildings in QGIS
Disk usage confusion: 10G missing on Linux home partition on SSD
What's the idiomatic (or best) way to trim surrounding whitespace from a string?
To “Er” Is Human
Making arrow with a gradual colour
Why will we fail creating a self sustaining off world colony?
What type of education should I select in this form?
Variable declaration inside main loop
Square wave to sawtooth wave using two BJT
Why is the saxophone not common in classical repertoire?
When does it become illegal to exchange bitcoin for cash?
I agreed to cancel a long-planned vacation (with travel costs) due to project deadlines, but now the timeline has all changed again
What is the meaning of ゴト in the context of 鮎
Why did the Middle Kingdom stop building pyramid tombs?
Is this house-rule removing the increased effect of cantrips at higher character levels balanced?
2019 2-letters 33-length list
Does Dhp 256-257 condone judging others?
Why is my 401k manager recommending me to save more?
How soon after takeoff can you recline your airplane seat?
Old story where computer expert digitally animates The Lord of the Rings
SQL Server Ignoring Instance name when using port number of different instance
Is it OK to throw pebbles and stones in streams, waterfalls, ponds, etc.?
Merging two data frames into a new one with unique items marked with 1 or 0
How to merge two dictionaries in a single expression?How to join (merge) data frames (inner, outer, left, right)Drop data frame columns by name“Large data” work flows using pandasMerging two data frames based on row values in python pandasMerge two python pandas data frames of different length but keep all rows in output data frameGet unmerged data from source tablesMerging two data frames with flexible conditionsCompare and equalize two data frames in PythonJoin two data frame with two columns values of a df with a single column values of another dataframe. based on some conditions?
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?
python pandas dataframe
add a comment |
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?
python pandas dataframe
just to make sure: in the final DataFrame you don't need anyCoeff
?
– Adam.Er8
9 hours ago
@Adam.Er8 thanks! I do not need them. Thanks!
– renakre
9 hours ago
add a comment |
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?
python pandas dataframe
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?
python pandas dataframe
python pandas dataframe
edited 7 hours ago
cs95
157k26 gold badges208 silver badges279 bronze badges
157k26 gold badges208 silver badges279 bronze badges
asked 9 hours ago
renakrerenakre
5,3591 gold badge16 silver badges53 bronze badges
5,3591 gold badge16 silver badges53 bronze badges
just to make sure: in the final DataFrame you don't need anyCoeff
?
– Adam.Er8
9 hours ago
@Adam.Er8 thanks! I do not need them. Thanks!
– renakre
9 hours ago
add a comment |
just to make sure: in the final DataFrame you don't need anyCoeff
?
– Adam.Er8
9 hours ago
@Adam.Er8 thanks! I do not need them. Thanks!
– renakre
9 hours ago
just to make sure: in the final DataFrame you don't need any
Coeff
?– Adam.Er8
9 hours ago
just to make sure: in the final DataFrame you don't need any
Coeff
?– Adam.Er8
9 hours ago
@Adam.Er8 thanks! I do not need them. Thanks!
– renakre
9 hours ago
@Adam.Er8 thanks! I do not need them. Thanks!
– renakre
9 hours ago
add a comment |
4 Answers
4
active
oldest
votes
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
add a comment |
You can use merge
with series.str.get_dummies()
together to achieve this:
m=df1[['Feature']].merge(df2[['Feature']],how='outer',indicator=True)
d='both':'DF1,DF2','left_only':'DF1','right_only':'DF2'
m=m.assign(_merge=m._merge.map(d))
m[['Feature']].join(m._merge.str.get_dummies(','))
Feature DF1 DF2
0 a 1 1
1 b 1 1
2 c 1 0
3 d 1 0
4 y 0 1
5 x 0 1
add a comment |
Same Idea like user3483203 but with crosstab
u = pd.concat([df1, df2], axis=0, keys=['DF1', 'DF2'])
pd.crosstab(u.Feature, u.index.get_level_values(0))
add a comment |
I merged two dataframes using pd.merge
and used list comprehension to assign values.
df = df1.merge(df2, on='Feature', how='outer')
df['DF1'] = [1 if x > 0 else 0 for x in df['Coeff_x']]
df['DF2'] = [1 if x > 0 else 0 for x in df['Coeff_y']]
df.drop(['Coeff_x', 'Coeff_y'], axis=1, inplace=True)
Feature DF1 DF2
0 a 1 1
1 b 1 1
2 c 1 0
3 d 1 0
4 y 0 1
5 x 0 1
I've seen other -- pandas specific-- answers, and I would like to ask what are the advantages of methods like series.str.get_dummies()
if you can achieve the same using built-in methods/functions? Is it much faster?
Genuinely curious since I'm a newbie myself.
(sorry I need more reputation points to leave comments directly under other answers!)
1
Not sure about performance, but you can assign the columns before the merge, then they get brought alongdf1.assign(DF1=1).merge(....)
Just need a.fillna
after :D
– ALollz
6 hours ago
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f56826251%2fmerging-two-data-frames-into-a-new-one-with-unique-items-marked-with-1-or-0%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
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
add a comment |
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
add a comment |
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
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
answered 9 hours ago
user3483203user3483203
35.1k8 gold badges31 silver badges60 bronze badges
35.1k8 gold badges31 silver badges60 bronze badges
add a comment |
add a comment |
You can use merge
with series.str.get_dummies()
together to achieve this:
m=df1[['Feature']].merge(df2[['Feature']],how='outer',indicator=True)
d='both':'DF1,DF2','left_only':'DF1','right_only':'DF2'
m=m.assign(_merge=m._merge.map(d))
m[['Feature']].join(m._merge.str.get_dummies(','))
Feature DF1 DF2
0 a 1 1
1 b 1 1
2 c 1 0
3 d 1 0
4 y 0 1
5 x 0 1
add a comment |
You can use merge
with series.str.get_dummies()
together to achieve this:
m=df1[['Feature']].merge(df2[['Feature']],how='outer',indicator=True)
d='both':'DF1,DF2','left_only':'DF1','right_only':'DF2'
m=m.assign(_merge=m._merge.map(d))
m[['Feature']].join(m._merge.str.get_dummies(','))
Feature DF1 DF2
0 a 1 1
1 b 1 1
2 c 1 0
3 d 1 0
4 y 0 1
5 x 0 1
add a comment |
You can use merge
with series.str.get_dummies()
together to achieve this:
m=df1[['Feature']].merge(df2[['Feature']],how='outer',indicator=True)
d='both':'DF1,DF2','left_only':'DF1','right_only':'DF2'
m=m.assign(_merge=m._merge.map(d))
m[['Feature']].join(m._merge.str.get_dummies(','))
Feature DF1 DF2
0 a 1 1
1 b 1 1
2 c 1 0
3 d 1 0
4 y 0 1
5 x 0 1
You can use merge
with series.str.get_dummies()
together to achieve this:
m=df1[['Feature']].merge(df2[['Feature']],how='outer',indicator=True)
d='both':'DF1,DF2','left_only':'DF1','right_only':'DF2'
m=m.assign(_merge=m._merge.map(d))
m[['Feature']].join(m._merge.str.get_dummies(','))
Feature DF1 DF2
0 a 1 1
1 b 1 1
2 c 1 0
3 d 1 0
4 y 0 1
5 x 0 1
answered 9 hours ago
anky_91anky_91
18.3k5 gold badges11 silver badges25 bronze badges
18.3k5 gold badges11 silver badges25 bronze badges
add a comment |
add a comment |
Same Idea like user3483203 but with crosstab
u = pd.concat([df1, df2], axis=0, keys=['DF1', 'DF2'])
pd.crosstab(u.Feature, u.index.get_level_values(0))
add a comment |
Same Idea like user3483203 but with crosstab
u = pd.concat([df1, df2], axis=0, keys=['DF1', 'DF2'])
pd.crosstab(u.Feature, u.index.get_level_values(0))
add a comment |
Same Idea like user3483203 but with crosstab
u = pd.concat([df1, df2], axis=0, keys=['DF1', 'DF2'])
pd.crosstab(u.Feature, u.index.get_level_values(0))
Same Idea like user3483203 but with crosstab
u = pd.concat([df1, df2], axis=0, keys=['DF1', 'DF2'])
pd.crosstab(u.Feature, u.index.get_level_values(0))
answered 8 hours ago
WeNYoBenWeNYoBen
144k8 gold badges51 silver badges80 bronze badges
144k8 gold badges51 silver badges80 bronze badges
add a comment |
add a comment |
I merged two dataframes using pd.merge
and used list comprehension to assign values.
df = df1.merge(df2, on='Feature', how='outer')
df['DF1'] = [1 if x > 0 else 0 for x in df['Coeff_x']]
df['DF2'] = [1 if x > 0 else 0 for x in df['Coeff_y']]
df.drop(['Coeff_x', 'Coeff_y'], axis=1, inplace=True)
Feature DF1 DF2
0 a 1 1
1 b 1 1
2 c 1 0
3 d 1 0
4 y 0 1
5 x 0 1
I've seen other -- pandas specific-- answers, and I would like to ask what are the advantages of methods like series.str.get_dummies()
if you can achieve the same using built-in methods/functions? Is it much faster?
Genuinely curious since I'm a newbie myself.
(sorry I need more reputation points to leave comments directly under other answers!)
1
Not sure about performance, but you can assign the columns before the merge, then they get brought alongdf1.assign(DF1=1).merge(....)
Just need a.fillna
after :D
– ALollz
6 hours ago
add a comment |
I merged two dataframes using pd.merge
and used list comprehension to assign values.
df = df1.merge(df2, on='Feature', how='outer')
df['DF1'] = [1 if x > 0 else 0 for x in df['Coeff_x']]
df['DF2'] = [1 if x > 0 else 0 for x in df['Coeff_y']]
df.drop(['Coeff_x', 'Coeff_y'], axis=1, inplace=True)
Feature DF1 DF2
0 a 1 1
1 b 1 1
2 c 1 0
3 d 1 0
4 y 0 1
5 x 0 1
I've seen other -- pandas specific-- answers, and I would like to ask what are the advantages of methods like series.str.get_dummies()
if you can achieve the same using built-in methods/functions? Is it much faster?
Genuinely curious since I'm a newbie myself.
(sorry I need more reputation points to leave comments directly under other answers!)
1
Not sure about performance, but you can assign the columns before the merge, then they get brought alongdf1.assign(DF1=1).merge(....)
Just need a.fillna
after :D
– ALollz
6 hours ago
add a comment |
I merged two dataframes using pd.merge
and used list comprehension to assign values.
df = df1.merge(df2, on='Feature', how='outer')
df['DF1'] = [1 if x > 0 else 0 for x in df['Coeff_x']]
df['DF2'] = [1 if x > 0 else 0 for x in df['Coeff_y']]
df.drop(['Coeff_x', 'Coeff_y'], axis=1, inplace=True)
Feature DF1 DF2
0 a 1 1
1 b 1 1
2 c 1 0
3 d 1 0
4 y 0 1
5 x 0 1
I've seen other -- pandas specific-- answers, and I would like to ask what are the advantages of methods like series.str.get_dummies()
if you can achieve the same using built-in methods/functions? Is it much faster?
Genuinely curious since I'm a newbie myself.
(sorry I need more reputation points to leave comments directly under other answers!)
I merged two dataframes using pd.merge
and used list comprehension to assign values.
df = df1.merge(df2, on='Feature', how='outer')
df['DF1'] = [1 if x > 0 else 0 for x in df['Coeff_x']]
df['DF2'] = [1 if x > 0 else 0 for x in df['Coeff_y']]
df.drop(['Coeff_x', 'Coeff_y'], axis=1, inplace=True)
Feature DF1 DF2
0 a 1 1
1 b 1 1
2 c 1 0
3 d 1 0
4 y 0 1
5 x 0 1
I've seen other -- pandas specific-- answers, and I would like to ask what are the advantages of methods like series.str.get_dummies()
if you can achieve the same using built-in methods/functions? Is it much faster?
Genuinely curious since I'm a newbie myself.
(sorry I need more reputation points to leave comments directly under other answers!)
answered 9 hours ago
political scientistpolitical scientist
658 bronze badges
658 bronze badges
1
Not sure about performance, but you can assign the columns before the merge, then they get brought alongdf1.assign(DF1=1).merge(....)
Just need a.fillna
after :D
– ALollz
6 hours ago
add a comment |
1
Not sure about performance, but you can assign the columns before the merge, then they get brought alongdf1.assign(DF1=1).merge(....)
Just need a.fillna
after :D
– ALollz
6 hours ago
1
1
Not sure about performance, but you can assign the columns before the merge, then they get brought along
df1.assign(DF1=1).merge(....)
Just need a .fillna
after :D– ALollz
6 hours ago
Not sure about performance, but you can assign the columns before the merge, then they get brought along
df1.assign(DF1=1).merge(....)
Just need a .fillna
after :D– ALollz
6 hours ago
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f56826251%2fmerging-two-data-frames-into-a-new-one-with-unique-items-marked-with-1-or-0%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
just to make sure: in the final DataFrame you don't need any
Coeff
?– Adam.Er8
9 hours ago
@Adam.Er8 thanks! I do not need them. Thanks!
– renakre
9 hours ago