How can I merge two data sets of different lengths in Python?












-1















I have tried merging with Pandas merge, however, as the length of data is different, merge function is broadcasting the data even when using a key.
The following line of code has been used.



dt = pd.merge(df,data[['Post ID','Sentiment']], on = 'Post ID')


Using join produces the following:



df.join(data[['Post ID','Sentiment']],on = 'Post ID')



You are trying to merge on object and int64 columns. If you wish to proceed you should use pd.concat











share|improve this question




















  • 1





    You should provide a Minimal, Complete, and Verifiable example.

    – jpp
    Jun 26 '18 at 8:35











  • Possible duplicate of Trying to merge 2 dataframes but get ValueError

    – Arault
    Jun 28 '18 at 8:13
















-1















I have tried merging with Pandas merge, however, as the length of data is different, merge function is broadcasting the data even when using a key.
The following line of code has been used.



dt = pd.merge(df,data[['Post ID','Sentiment']], on = 'Post ID')


Using join produces the following:



df.join(data[['Post ID','Sentiment']],on = 'Post ID')



You are trying to merge on object and int64 columns. If you wish to proceed you should use pd.concat











share|improve this question




















  • 1





    You should provide a Minimal, Complete, and Verifiable example.

    – jpp
    Jun 26 '18 at 8:35











  • Possible duplicate of Trying to merge 2 dataframes but get ValueError

    – Arault
    Jun 28 '18 at 8:13














-1












-1








-1








I have tried merging with Pandas merge, however, as the length of data is different, merge function is broadcasting the data even when using a key.
The following line of code has been used.



dt = pd.merge(df,data[['Post ID','Sentiment']], on = 'Post ID')


Using join produces the following:



df.join(data[['Post ID','Sentiment']],on = 'Post ID')



You are trying to merge on object and int64 columns. If you wish to proceed you should use pd.concat











share|improve this question
















I have tried merging with Pandas merge, however, as the length of data is different, merge function is broadcasting the data even when using a key.
The following line of code has been used.



dt = pd.merge(df,data[['Post ID','Sentiment']], on = 'Post ID')


Using join produces the following:



df.join(data[['Post ID','Sentiment']],on = 'Post ID')



You are trying to merge on object and int64 columns. If you wish to proceed you should use pd.concat








pandas dataframe






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 1 at 9:06







Parth Batra

















asked Jun 26 '18 at 8:17









Parth BatraParth Batra

124




124








  • 1





    You should provide a Minimal, Complete, and Verifiable example.

    – jpp
    Jun 26 '18 at 8:35











  • Possible duplicate of Trying to merge 2 dataframes but get ValueError

    – Arault
    Jun 28 '18 at 8:13














  • 1





    You should provide a Minimal, Complete, and Verifiable example.

    – jpp
    Jun 26 '18 at 8:35











  • Possible duplicate of Trying to merge 2 dataframes but get ValueError

    – Arault
    Jun 28 '18 at 8:13








1




1





You should provide a Minimal, Complete, and Verifiable example.

– jpp
Jun 26 '18 at 8:35





You should provide a Minimal, Complete, and Verifiable example.

– jpp
Jun 26 '18 at 8:35













Possible duplicate of Trying to merge 2 dataframes but get ValueError

– Arault
Jun 28 '18 at 8:13





Possible duplicate of Trying to merge 2 dataframes but get ValueError

– Arault
Jun 28 '18 at 8:13












1 Answer
1






active

oldest

votes


















0














This error means that in one of your database, Post ID is an objectand in the other one it is defined as int.
You need to convert them so they have the same type, for instance by doing :



df['Post ID'] = df['Post ID'].astype(int)





share|improve this answer


























  • I have tried changing the post ID type to string as Post ID contains both strings and integers. However, the error persists

    – Parth Batra
    Jun 26 '18 at 10:30













  • Could you provide data so we can reproduce your error ?

    – Arault
    Jun 26 '18 at 10:38











  • Sorry, I cannot provide the data as it is from a client.

    – Parth Batra
    Jun 28 '18 at 7:27











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


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f51038096%2fhow-can-i-merge-two-data-sets-of-different-lengths-in-python%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









0














This error means that in one of your database, Post ID is an objectand in the other one it is defined as int.
You need to convert them so they have the same type, for instance by doing :



df['Post ID'] = df['Post ID'].astype(int)





share|improve this answer


























  • I have tried changing the post ID type to string as Post ID contains both strings and integers. However, the error persists

    – Parth Batra
    Jun 26 '18 at 10:30













  • Could you provide data so we can reproduce your error ?

    – Arault
    Jun 26 '18 at 10:38











  • Sorry, I cannot provide the data as it is from a client.

    – Parth Batra
    Jun 28 '18 at 7:27
















0














This error means that in one of your database, Post ID is an objectand in the other one it is defined as int.
You need to convert them so they have the same type, for instance by doing :



df['Post ID'] = df['Post ID'].astype(int)





share|improve this answer


























  • I have tried changing the post ID type to string as Post ID contains both strings and integers. However, the error persists

    – Parth Batra
    Jun 26 '18 at 10:30













  • Could you provide data so we can reproduce your error ?

    – Arault
    Jun 26 '18 at 10:38











  • Sorry, I cannot provide the data as it is from a client.

    – Parth Batra
    Jun 28 '18 at 7:27














0












0








0







This error means that in one of your database, Post ID is an objectand in the other one it is defined as int.
You need to convert them so they have the same type, for instance by doing :



df['Post ID'] = df['Post ID'].astype(int)





share|improve this answer















This error means that in one of your database, Post ID is an objectand in the other one it is defined as int.
You need to convert them so they have the same type, for instance by doing :



df['Post ID'] = df['Post ID'].astype(int)






share|improve this answer














share|improve this answer



share|improve this answer








edited Jun 26 '18 at 8:36









eyllanesc

79.2k103257




79.2k103257










answered Jun 26 '18 at 8:23









AraultArault

46729




46729













  • I have tried changing the post ID type to string as Post ID contains both strings and integers. However, the error persists

    – Parth Batra
    Jun 26 '18 at 10:30













  • Could you provide data so we can reproduce your error ?

    – Arault
    Jun 26 '18 at 10:38











  • Sorry, I cannot provide the data as it is from a client.

    – Parth Batra
    Jun 28 '18 at 7:27



















  • I have tried changing the post ID type to string as Post ID contains both strings and integers. However, the error persists

    – Parth Batra
    Jun 26 '18 at 10:30













  • Could you provide data so we can reproduce your error ?

    – Arault
    Jun 26 '18 at 10:38











  • Sorry, I cannot provide the data as it is from a client.

    – Parth Batra
    Jun 28 '18 at 7:27

















I have tried changing the post ID type to string as Post ID contains both strings and integers. However, the error persists

– Parth Batra
Jun 26 '18 at 10:30







I have tried changing the post ID type to string as Post ID contains both strings and integers. However, the error persists

– Parth Batra
Jun 26 '18 at 10:30















Could you provide data so we can reproduce your error ?

– Arault
Jun 26 '18 at 10:38





Could you provide data so we can reproduce your error ?

– Arault
Jun 26 '18 at 10:38













Sorry, I cannot provide the data as it is from a client.

– Parth Batra
Jun 28 '18 at 7:27





Sorry, I cannot provide the data as it is from a client.

– Parth Batra
Jun 28 '18 at 7:27




















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.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f51038096%2fhow-can-i-merge-two-data-sets-of-different-lengths-in-python%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

Monofisismo

Angular Downloading a file using contenturl with Basic Authentication

Olmecas