Pandas merging two dataframes with different number of multiindices











up vote
0
down vote

favorite












Welcome, I have a simple question, to which I haven't found a solution.



I have two dataframes df1 and df2:





  • df1 contains several columns and a multiindex as year-month-week


  • df2 contains the multiindex year-week with only one column in the df.




I would like to create an inner join of df1 and df2, joining on 'year' and 'week'.





I have tried to do the following:



df1['newcol'] = df1.index.get_level_values(2).map(lambda x: df2.newcol[x])


Which only joins on month (or year?), is there any way to expand it so that the merge is actually right?



Thanks in advance!



df1



df2










share|improve this question




















  • 2




    Can you provide a sample dataframe via a Minimal, Complete, and Verifiable example.
    – jpp
    Nov 22 at 15:10






  • 2




    Show us some sample of the datasets. Refer to the guide on how to add code snippets.
    – Shiv_90
    Nov 22 at 15:11










  • I added some pictures to represent the dataset, i hope it helps!
    – acronis011
    Nov 22 at 15:22










  • We can't copy/paste pictures into python :D
    – user3471881
    Nov 22 at 15:32






  • 1




    Did you try pd.merge([df1, df2], how = 'inner', on = ['year', 'week']?
    – maow
    Nov 22 at 15:38

















up vote
0
down vote

favorite












Welcome, I have a simple question, to which I haven't found a solution.



I have two dataframes df1 and df2:





  • df1 contains several columns and a multiindex as year-month-week


  • df2 contains the multiindex year-week with only one column in the df.




I would like to create an inner join of df1 and df2, joining on 'year' and 'week'.





I have tried to do the following:



df1['newcol'] = df1.index.get_level_values(2).map(lambda x: df2.newcol[x])


Which only joins on month (or year?), is there any way to expand it so that the merge is actually right?



Thanks in advance!



df1



df2










share|improve this question




















  • 2




    Can you provide a sample dataframe via a Minimal, Complete, and Verifiable example.
    – jpp
    Nov 22 at 15:10






  • 2




    Show us some sample of the datasets. Refer to the guide on how to add code snippets.
    – Shiv_90
    Nov 22 at 15:11










  • I added some pictures to represent the dataset, i hope it helps!
    – acronis011
    Nov 22 at 15:22










  • We can't copy/paste pictures into python :D
    – user3471881
    Nov 22 at 15:32






  • 1




    Did you try pd.merge([df1, df2], how = 'inner', on = ['year', 'week']?
    – maow
    Nov 22 at 15:38















up vote
0
down vote

favorite









up vote
0
down vote

favorite











Welcome, I have a simple question, to which I haven't found a solution.



I have two dataframes df1 and df2:





  • df1 contains several columns and a multiindex as year-month-week


  • df2 contains the multiindex year-week with only one column in the df.




I would like to create an inner join of df1 and df2, joining on 'year' and 'week'.





I have tried to do the following:



df1['newcol'] = df1.index.get_level_values(2).map(lambda x: df2.newcol[x])


Which only joins on month (or year?), is there any way to expand it so that the merge is actually right?



Thanks in advance!



df1



df2










share|improve this question















Welcome, I have a simple question, to which I haven't found a solution.



I have two dataframes df1 and df2:





  • df1 contains several columns and a multiindex as year-month-week


  • df2 contains the multiindex year-week with only one column in the df.




I would like to create an inner join of df1 and df2, joining on 'year' and 'week'.





I have tried to do the following:



df1['newcol'] = df1.index.get_level_values(2).map(lambda x: df2.newcol[x])


Which only joins on month (or year?), is there any way to expand it so that the merge is actually right?



Thanks in advance!



df1



df2







python pandas dataframe






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 22 at 15:20

























asked Nov 22 at 15:09









acronis011

33




33








  • 2




    Can you provide a sample dataframe via a Minimal, Complete, and Verifiable example.
    – jpp
    Nov 22 at 15:10






  • 2




    Show us some sample of the datasets. Refer to the guide on how to add code snippets.
    – Shiv_90
    Nov 22 at 15:11










  • I added some pictures to represent the dataset, i hope it helps!
    – acronis011
    Nov 22 at 15:22










  • We can't copy/paste pictures into python :D
    – user3471881
    Nov 22 at 15:32






  • 1




    Did you try pd.merge([df1, df2], how = 'inner', on = ['year', 'week']?
    – maow
    Nov 22 at 15:38
















  • 2




    Can you provide a sample dataframe via a Minimal, Complete, and Verifiable example.
    – jpp
    Nov 22 at 15:10






  • 2




    Show us some sample of the datasets. Refer to the guide on how to add code snippets.
    – Shiv_90
    Nov 22 at 15:11










  • I added some pictures to represent the dataset, i hope it helps!
    – acronis011
    Nov 22 at 15:22










  • We can't copy/paste pictures into python :D
    – user3471881
    Nov 22 at 15:32






  • 1




    Did you try pd.merge([df1, df2], how = 'inner', on = ['year', 'week']?
    – maow
    Nov 22 at 15:38










2




2




Can you provide a sample dataframe via a Minimal, Complete, and Verifiable example.
– jpp
Nov 22 at 15:10




Can you provide a sample dataframe via a Minimal, Complete, and Verifiable example.
– jpp
Nov 22 at 15:10




2




2




Show us some sample of the datasets. Refer to the guide on how to add code snippets.
– Shiv_90
Nov 22 at 15:11




Show us some sample of the datasets. Refer to the guide on how to add code snippets.
– Shiv_90
Nov 22 at 15:11












I added some pictures to represent the dataset, i hope it helps!
– acronis011
Nov 22 at 15:22




I added some pictures to represent the dataset, i hope it helps!
– acronis011
Nov 22 at 15:22












We can't copy/paste pictures into python :D
– user3471881
Nov 22 at 15:32




We can't copy/paste pictures into python :D
– user3471881
Nov 22 at 15:32




1




1




Did you try pd.merge([df1, df2], how = 'inner', on = ['year', 'week']?
– maow
Nov 22 at 15:38






Did you try pd.merge([df1, df2], how = 'inner', on = ['year', 'week']?
– maow
Nov 22 at 15:38














1 Answer
1






active

oldest

votes

















up vote
0
down vote













Eventually i solved with with removing the multiindex and doing a good old inner join on the two columns and then recreating the multiindex at the end.
Here are the sniplets:



df=df.reset_index()



df2=df2.reset_index()



df['year']=df['year'].apply(int)



df2['year']=df2['year'].apply(int)



df['week']=df['week'].apply(int)



df2['week']=df2['week'].apply(int)



result = pd.merge(df, df2, how='left', left_on= ['year','week'],right_on= ['year','week'])



result=result.set_index(['year', 'month','week','day'])






share|improve this answer























  • Since flattening index is not that intuitive for many, I'd suggest that you could add the actual code that you used, so that others could benefit from your experience ;) stackoverflow.blog/2011/07/01/…
    – leoburgy
    Nov 22 at 16:08











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',
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
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53433795%2fpandas-merging-two-dataframes-with-different-number-of-multiindices%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
0
down vote













Eventually i solved with with removing the multiindex and doing a good old inner join on the two columns and then recreating the multiindex at the end.
Here are the sniplets:



df=df.reset_index()



df2=df2.reset_index()



df['year']=df['year'].apply(int)



df2['year']=df2['year'].apply(int)



df['week']=df['week'].apply(int)



df2['week']=df2['week'].apply(int)



result = pd.merge(df, df2, how='left', left_on= ['year','week'],right_on= ['year','week'])



result=result.set_index(['year', 'month','week','day'])






share|improve this answer























  • Since flattening index is not that intuitive for many, I'd suggest that you could add the actual code that you used, so that others could benefit from your experience ;) stackoverflow.blog/2011/07/01/…
    – leoburgy
    Nov 22 at 16:08















up vote
0
down vote













Eventually i solved with with removing the multiindex and doing a good old inner join on the two columns and then recreating the multiindex at the end.
Here are the sniplets:



df=df.reset_index()



df2=df2.reset_index()



df['year']=df['year'].apply(int)



df2['year']=df2['year'].apply(int)



df['week']=df['week'].apply(int)



df2['week']=df2['week'].apply(int)



result = pd.merge(df, df2, how='left', left_on= ['year','week'],right_on= ['year','week'])



result=result.set_index(['year', 'month','week','day'])






share|improve this answer























  • Since flattening index is not that intuitive for many, I'd suggest that you could add the actual code that you used, so that others could benefit from your experience ;) stackoverflow.blog/2011/07/01/…
    – leoburgy
    Nov 22 at 16:08













up vote
0
down vote










up vote
0
down vote









Eventually i solved with with removing the multiindex and doing a good old inner join on the two columns and then recreating the multiindex at the end.
Here are the sniplets:



df=df.reset_index()



df2=df2.reset_index()



df['year']=df['year'].apply(int)



df2['year']=df2['year'].apply(int)



df['week']=df['week'].apply(int)



df2['week']=df2['week'].apply(int)



result = pd.merge(df, df2, how='left', left_on= ['year','week'],right_on= ['year','week'])



result=result.set_index(['year', 'month','week','day'])






share|improve this answer














Eventually i solved with with removing the multiindex and doing a good old inner join on the two columns and then recreating the multiindex at the end.
Here are the sniplets:



df=df.reset_index()



df2=df2.reset_index()



df['year']=df['year'].apply(int)



df2['year']=df2['year'].apply(int)



df['week']=df['week'].apply(int)



df2['week']=df2['week'].apply(int)



result = pd.merge(df, df2, how='left', left_on= ['year','week'],right_on= ['year','week'])



result=result.set_index(['year', 'month','week','day'])







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 22 at 16:20

























answered Nov 22 at 15:51









acronis011

33




33












  • Since flattening index is not that intuitive for many, I'd suggest that you could add the actual code that you used, so that others could benefit from your experience ;) stackoverflow.blog/2011/07/01/…
    – leoburgy
    Nov 22 at 16:08


















  • Since flattening index is not that intuitive for many, I'd suggest that you could add the actual code that you used, so that others could benefit from your experience ;) stackoverflow.blog/2011/07/01/…
    – leoburgy
    Nov 22 at 16:08
















Since flattening index is not that intuitive for many, I'd suggest that you could add the actual code that you used, so that others could benefit from your experience ;) stackoverflow.blog/2011/07/01/…
– leoburgy
Nov 22 at 16:08




Since flattening index is not that intuitive for many, I'd suggest that you could add the actual code that you used, so that others could benefit from your experience ;) stackoverflow.blog/2011/07/01/…
– leoburgy
Nov 22 at 16:08


















draft saved

draft discarded




















































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.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • 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.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53433795%2fpandas-merging-two-dataframes-with-different-number-of-multiindices%23new-answer', 'question_page');
}
);

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







Popular posts from this blog

What visual should I use to simply compare current year value vs last year in Power BI desktop

How to ignore python UserWarning in pytest?

Alexandru Averescu