compare pandas dataframes with different lengths The 2019 Stack Overflow Developer Survey Results Are InWhere in the workflow should we deal with missing data?pandas dataframes memoryPlotting different values in pandas histogram with different colorsCreate a new column based on two columns from two different dataframesConcatenate dataframes PandasCombine Pandas DataFrames with year columnsSpearmanr on two pandas dataframesPrediction based on more dataframesPandas: How can I merge two dataframes?Joining two dataframes on the basis of specific conditions

Multi tool use
How to translate "being like"?
Why doesn't mkfifo with a mode of 1755 grant read permissions and sticky bit to the user?
Should I use my personal e-mail address, or my workplace one, when registering to external websites for work purposes?
Pokemon Turn Based battle (Python)
Can you compress metal and what would be the consequences?
Why can't devices on different VLANs, but on the same subnet, communicate?
Is an up-to-date browser secure on an out-of-date OS?
Why was M87 targeted for the Event Horizon Telescope instead of Sagittarius A*?
Deal with toxic manager when you can't quit
If I score a critical hit on an 18 or higher, what are my chances of getting a critical hit if I roll 3d20?
Why isn't the circumferential light around the M87 black hole's event horizon symmetric?
Why isn't airport relocation done gradually?
"as much details as you can remember"
If I can cast sorceries at instant speed, can I use sorcery-speed activated abilities at instant speed?
I am eight letters word. Find me who Am I?
With regards to an effect that triggers when a creature attacks, how does it entering the battlefield tapped and attacking apply?
slides for 30min~1hr skype tenure track application interview
How come people say “Would of”?
Can there be female White Walkers?
Is it ethical to upload a automatically generated paper to a non peer-reviewed site as part of a larger research?
Why not take a picture of a closer black hole?
Can a flute soloist sit?
How can I add encounters in the Lost Mine of Phandelver campaign without giving PCs too much XP?
"consumers choosing to rely" vs. "consumers to choose to rely"
compare pandas dataframes with different lengths
The 2019 Stack Overflow Developer Survey Results Are InWhere in the workflow should we deal with missing data?pandas dataframes memoryPlotting different values in pandas histogram with different colorsCreate a new column based on two columns from two different dataframesConcatenate dataframes PandasCombine Pandas DataFrames with year columnsSpearmanr on two pandas dataframesPrediction based on more dataframesPandas: How can I merge two dataframes?Joining two dataframes on the basis of specific conditions
$begingroup$
I have two dataframes of different lengths and I need to add a column to the first one with filtered values, e.g.
df1 = pd.DataFrame('Object':['cup', 'brick', 'board', 'stone'], 'id':[2, 8, 9, 6])
df1 = pd.DataFrame('Thing':['cup', 'board'], 'color':['blue', 'grey'])
and I want to create
df = pd.DataFrame('Thing':['cup', 'board'], 'color':['blue', 'grey'], 'id':[2, 9])
All methods I tried to use complained about different lengths.
pandas
$endgroup$
add a comment |
$begingroup$
I have two dataframes of different lengths and I need to add a column to the first one with filtered values, e.g.
df1 = pd.DataFrame('Object':['cup', 'brick', 'board', 'stone'], 'id':[2, 8, 9, 6])
df1 = pd.DataFrame('Thing':['cup', 'board'], 'color':['blue', 'grey'])
and I want to create
df = pd.DataFrame('Thing':['cup', 'board'], 'color':['blue', 'grey'], 'id':[2, 9])
All methods I tried to use complained about different lengths.
pandas
$endgroup$
add a comment |
$begingroup$
I have two dataframes of different lengths and I need to add a column to the first one with filtered values, e.g.
df1 = pd.DataFrame('Object':['cup', 'brick', 'board', 'stone'], 'id':[2, 8, 9, 6])
df1 = pd.DataFrame('Thing':['cup', 'board'], 'color':['blue', 'grey'])
and I want to create
df = pd.DataFrame('Thing':['cup', 'board'], 'color':['blue', 'grey'], 'id':[2, 9])
All methods I tried to use complained about different lengths.
pandas
$endgroup$
I have two dataframes of different lengths and I need to add a column to the first one with filtered values, e.g.
df1 = pd.DataFrame('Object':['cup', 'brick', 'board', 'stone'], 'id':[2, 8, 9, 6])
df1 = pd.DataFrame('Thing':['cup', 'board'], 'color':['blue', 'grey'])
and I want to create
df = pd.DataFrame('Thing':['cup', 'board'], 'color':['blue', 'grey'], 'id':[2, 9])
All methods I tried to use complained about different lengths.
pandas
pandas
edited Mar 30 at 11:06
Grw Křemílek
asked Mar 30 at 8:25


Grw KřemílekGrw Křemílek
13
13
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
$begingroup$
You can accomplish your task by using the merge
operation in pandas as follows:
In [16]: df1 = pd.DataFrame('Object':['cup', 'brick', 'board', 'stone'], 'id':[2, 8, 9, 6])
In [17]: df1
Out[17]:
Object id
0 cup 2
1 brick 8
2 board 9
3 stone 6
In [18]: df2 = pd.DataFrame('Thing':['cup', 'board'], 'color':['blue', 'grey'])
In [19]: df2
Out[19]:
Thing color
0 cup blue
1 board grey
In [20]: df = df2.merge(df1, left_on='Thing',right_on='Object', how='inner')
In [21]: df
Out[21]:
Thing color Object id
0 cup blue cup 2
1 board grey board 9
and then drop the column (df.drop('Object', inplace=True)
) that you don't need. For more details look at the official documentation here. Also, check out this to see how you can use merge and join operations in pandas to do all kinds of dataframe manipulations!
$endgroup$
$begingroup$
that did it, thank you
$endgroup$
– Grw Křemílek
Mar 30 at 11:16
$begingroup$
You may select it as the answer if your problem is solved.
$endgroup$
– bkshi
Mar 30 at 11:43
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
return StackExchange.using("mathjaxEditing", function ()
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["$", "$"], ["\\(","\\)"]]);
);
);
, "mathjax-editing");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "557"
;
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: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
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%2fdatascience.stackexchange.com%2fquestions%2f48250%2fcompare-pandas-dataframes-with-different-lengths%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
$begingroup$
You can accomplish your task by using the merge
operation in pandas as follows:
In [16]: df1 = pd.DataFrame('Object':['cup', 'brick', 'board', 'stone'], 'id':[2, 8, 9, 6])
In [17]: df1
Out[17]:
Object id
0 cup 2
1 brick 8
2 board 9
3 stone 6
In [18]: df2 = pd.DataFrame('Thing':['cup', 'board'], 'color':['blue', 'grey'])
In [19]: df2
Out[19]:
Thing color
0 cup blue
1 board grey
In [20]: df = df2.merge(df1, left_on='Thing',right_on='Object', how='inner')
In [21]: df
Out[21]:
Thing color Object id
0 cup blue cup 2
1 board grey board 9
and then drop the column (df.drop('Object', inplace=True)
) that you don't need. For more details look at the official documentation here. Also, check out this to see how you can use merge and join operations in pandas to do all kinds of dataframe manipulations!
$endgroup$
$begingroup$
that did it, thank you
$endgroup$
– Grw Křemílek
Mar 30 at 11:16
$begingroup$
You may select it as the answer if your problem is solved.
$endgroup$
– bkshi
Mar 30 at 11:43
add a comment |
$begingroup$
You can accomplish your task by using the merge
operation in pandas as follows:
In [16]: df1 = pd.DataFrame('Object':['cup', 'brick', 'board', 'stone'], 'id':[2, 8, 9, 6])
In [17]: df1
Out[17]:
Object id
0 cup 2
1 brick 8
2 board 9
3 stone 6
In [18]: df2 = pd.DataFrame('Thing':['cup', 'board'], 'color':['blue', 'grey'])
In [19]: df2
Out[19]:
Thing color
0 cup blue
1 board grey
In [20]: df = df2.merge(df1, left_on='Thing',right_on='Object', how='inner')
In [21]: df
Out[21]:
Thing color Object id
0 cup blue cup 2
1 board grey board 9
and then drop the column (df.drop('Object', inplace=True)
) that you don't need. For more details look at the official documentation here. Also, check out this to see how you can use merge and join operations in pandas to do all kinds of dataframe manipulations!
$endgroup$
$begingroup$
that did it, thank you
$endgroup$
– Grw Křemílek
Mar 30 at 11:16
$begingroup$
You may select it as the answer if your problem is solved.
$endgroup$
– bkshi
Mar 30 at 11:43
add a comment |
$begingroup$
You can accomplish your task by using the merge
operation in pandas as follows:
In [16]: df1 = pd.DataFrame('Object':['cup', 'brick', 'board', 'stone'], 'id':[2, 8, 9, 6])
In [17]: df1
Out[17]:
Object id
0 cup 2
1 brick 8
2 board 9
3 stone 6
In [18]: df2 = pd.DataFrame('Thing':['cup', 'board'], 'color':['blue', 'grey'])
In [19]: df2
Out[19]:
Thing color
0 cup blue
1 board grey
In [20]: df = df2.merge(df1, left_on='Thing',right_on='Object', how='inner')
In [21]: df
Out[21]:
Thing color Object id
0 cup blue cup 2
1 board grey board 9
and then drop the column (df.drop('Object', inplace=True)
) that you don't need. For more details look at the official documentation here. Also, check out this to see how you can use merge and join operations in pandas to do all kinds of dataframe manipulations!
$endgroup$
You can accomplish your task by using the merge
operation in pandas as follows:
In [16]: df1 = pd.DataFrame('Object':['cup', 'brick', 'board', 'stone'], 'id':[2, 8, 9, 6])
In [17]: df1
Out[17]:
Object id
0 cup 2
1 brick 8
2 board 9
3 stone 6
In [18]: df2 = pd.DataFrame('Thing':['cup', 'board'], 'color':['blue', 'grey'])
In [19]: df2
Out[19]:
Thing color
0 cup blue
1 board grey
In [20]: df = df2.merge(df1, left_on='Thing',right_on='Object', how='inner')
In [21]: df
Out[21]:
Thing color Object id
0 cup blue cup 2
1 board grey board 9
and then drop the column (df.drop('Object', inplace=True)
) that you don't need. For more details look at the official documentation here. Also, check out this to see how you can use merge and join operations in pandas to do all kinds of dataframe manipulations!
answered Mar 30 at 8:46


bkshibkshi
758212
758212
$begingroup$
that did it, thank you
$endgroup$
– Grw Křemílek
Mar 30 at 11:16
$begingroup$
You may select it as the answer if your problem is solved.
$endgroup$
– bkshi
Mar 30 at 11:43
add a comment |
$begingroup$
that did it, thank you
$endgroup$
– Grw Křemílek
Mar 30 at 11:16
$begingroup$
You may select it as the answer if your problem is solved.
$endgroup$
– bkshi
Mar 30 at 11:43
$begingroup$
that did it, thank you
$endgroup$
– Grw Křemílek
Mar 30 at 11:16
$begingroup$
that did it, thank you
$endgroup$
– Grw Křemílek
Mar 30 at 11:16
$begingroup$
You may select it as the answer if your problem is solved.
$endgroup$
– bkshi
Mar 30 at 11:43
$begingroup$
You may select it as the answer if your problem is solved.
$endgroup$
– bkshi
Mar 30 at 11:43
add a comment |
Thanks for contributing an answer to Data Science Stack Exchange!
- 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.
Use MathJax to format equations. MathJax reference.
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%2fdatascience.stackexchange.com%2fquestions%2f48250%2fcompare-pandas-dataframes-with-different-lengths%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
2MiGFlC4dVyUb,ORI