How to Join CSV in Python Pandas Comparing 2 CSV
I have 2 csv files lets say A.csv and B.csv. A.csv has columns a,b,c,d and B.csv has columns x,y,z,t. I want to search that if an entry in column a exist in column x then print z and d if that rows.
Like,
for each i in A
if A.[a][i] exist in B.x
print A.[d][i] + B.[z][i]
-- I have already the code below. I just need to learn how to settle the code for this
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
A = pd.read_csv('path1')
B = pd.read_csv('path2')
python pandas csv numpy
add a comment |
I have 2 csv files lets say A.csv and B.csv. A.csv has columns a,b,c,d and B.csv has columns x,y,z,t. I want to search that if an entry in column a exist in column x then print z and d if that rows.
Like,
for each i in A
if A.[a][i] exist in B.x
print A.[d][i] + B.[z][i]
-- I have already the code below. I just need to learn how to settle the code for this
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
A = pd.read_csv('path1')
B = pd.read_csv('path2')
python pandas csv numpy
1
Could you please provide sample dataframes forA
andB
and an expected output?
– Scotty1-
Nov 15 '18 at 13:55
add a comment |
I have 2 csv files lets say A.csv and B.csv. A.csv has columns a,b,c,d and B.csv has columns x,y,z,t. I want to search that if an entry in column a exist in column x then print z and d if that rows.
Like,
for each i in A
if A.[a][i] exist in B.x
print A.[d][i] + B.[z][i]
-- I have already the code below. I just need to learn how to settle the code for this
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
A = pd.read_csv('path1')
B = pd.read_csv('path2')
python pandas csv numpy
I have 2 csv files lets say A.csv and B.csv. A.csv has columns a,b,c,d and B.csv has columns x,y,z,t. I want to search that if an entry in column a exist in column x then print z and d if that rows.
Like,
for each i in A
if A.[a][i] exist in B.x
print A.[d][i] + B.[z][i]
-- I have already the code below. I just need to learn how to settle the code for this
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
A = pd.read_csv('path1')
B = pd.read_csv('path2')
python pandas csv numpy
python pandas csv numpy
edited Nov 15 '18 at 14:04
chthonicdaemon
12.5k3147
12.5k3147
asked Nov 15 '18 at 13:54
abidinberkayabidinberkay
62411031
62411031
1
Could you please provide sample dataframes forA
andB
and an expected output?
– Scotty1-
Nov 15 '18 at 13:55
add a comment |
1
Could you please provide sample dataframes forA
andB
and an expected output?
– Scotty1-
Nov 15 '18 at 13:55
1
1
Could you please provide sample dataframes for
A
and B
and an expected output?– Scotty1-
Nov 15 '18 at 13:55
Could you please provide sample dataframes for
A
and B
and an expected output?– Scotty1-
Nov 15 '18 at 13:55
add a comment |
2 Answers
2
active
oldest
votes
Imagine your csv data file looks like below:
print(df1)
A B C D
0 1 4 7 4
1 2 5 8 5
2 3 6 9 8
print(df2
X Y Z T
0 1 11 6 8
1 5 12 8 0
2 2 13 0 4
A simple merge would solve your problem, considering Left table is df1 and Right is df2
df = df1.merge(df2,left_on='A',right_on='X')[['Z','D']]
print(df)
Z D
0 6 4
1 0 5
This will return the matched row elements from mentioned columns. (here Z,D)
add a comment |
I think the simplest solution is to use a left join:
>>> print(A)
a b c d
0 1 2 3 4
1 2 3 4 5
2 4 4 5 6
>>> print(B)
x y z t
0 1 20 30 40
1 3 4 5 6
>>> result = A.merge(left_on='a', right=B, right_on='x', how='left')[['z', 'd']].dropna()
>>> print(result)
z d
0 30.0 4
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%2f53321027%2fhow-to-join-csv-in-python-pandas-comparing-2-csv%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Imagine your csv data file looks like below:
print(df1)
A B C D
0 1 4 7 4
1 2 5 8 5
2 3 6 9 8
print(df2
X Y Z T
0 1 11 6 8
1 5 12 8 0
2 2 13 0 4
A simple merge would solve your problem, considering Left table is df1 and Right is df2
df = df1.merge(df2,left_on='A',right_on='X')[['Z','D']]
print(df)
Z D
0 6 4
1 0 5
This will return the matched row elements from mentioned columns. (here Z,D)
add a comment |
Imagine your csv data file looks like below:
print(df1)
A B C D
0 1 4 7 4
1 2 5 8 5
2 3 6 9 8
print(df2
X Y Z T
0 1 11 6 8
1 5 12 8 0
2 2 13 0 4
A simple merge would solve your problem, considering Left table is df1 and Right is df2
df = df1.merge(df2,left_on='A',right_on='X')[['Z','D']]
print(df)
Z D
0 6 4
1 0 5
This will return the matched row elements from mentioned columns. (here Z,D)
add a comment |
Imagine your csv data file looks like below:
print(df1)
A B C D
0 1 4 7 4
1 2 5 8 5
2 3 6 9 8
print(df2
X Y Z T
0 1 11 6 8
1 5 12 8 0
2 2 13 0 4
A simple merge would solve your problem, considering Left table is df1 and Right is df2
df = df1.merge(df2,left_on='A',right_on='X')[['Z','D']]
print(df)
Z D
0 6 4
1 0 5
This will return the matched row elements from mentioned columns. (here Z,D)
Imagine your csv data file looks like below:
print(df1)
A B C D
0 1 4 7 4
1 2 5 8 5
2 3 6 9 8
print(df2
X Y Z T
0 1 11 6 8
1 5 12 8 0
2 2 13 0 4
A simple merge would solve your problem, considering Left table is df1 and Right is df2
df = df1.merge(df2,left_on='A',right_on='X')[['Z','D']]
print(df)
Z D
0 6 4
1 0 5
This will return the matched row elements from mentioned columns. (here Z,D)
answered Nov 15 '18 at 17:22
ManojNiraleManojNirale
13410
13410
add a comment |
add a comment |
I think the simplest solution is to use a left join:
>>> print(A)
a b c d
0 1 2 3 4
1 2 3 4 5
2 4 4 5 6
>>> print(B)
x y z t
0 1 20 30 40
1 3 4 5 6
>>> result = A.merge(left_on='a', right=B, right_on='x', how='left')[['z', 'd']].dropna()
>>> print(result)
z d
0 30.0 4
add a comment |
I think the simplest solution is to use a left join:
>>> print(A)
a b c d
0 1 2 3 4
1 2 3 4 5
2 4 4 5 6
>>> print(B)
x y z t
0 1 20 30 40
1 3 4 5 6
>>> result = A.merge(left_on='a', right=B, right_on='x', how='left')[['z', 'd']].dropna()
>>> print(result)
z d
0 30.0 4
add a comment |
I think the simplest solution is to use a left join:
>>> print(A)
a b c d
0 1 2 3 4
1 2 3 4 5
2 4 4 5 6
>>> print(B)
x y z t
0 1 20 30 40
1 3 4 5 6
>>> result = A.merge(left_on='a', right=B, right_on='x', how='left')[['z', 'd']].dropna()
>>> print(result)
z d
0 30.0 4
I think the simplest solution is to use a left join:
>>> print(A)
a b c d
0 1 2 3 4
1 2 3 4 5
2 4 4 5 6
>>> print(B)
x y z t
0 1 20 30 40
1 3 4 5 6
>>> result = A.merge(left_on='a', right=B, right_on='x', how='left')[['z', 'd']].dropna()
>>> print(result)
z d
0 30.0 4
answered Nov 15 '18 at 14:03
chthonicdaemonchthonicdaemon
12.5k3147
12.5k3147
add a comment |
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%2f53321027%2fhow-to-join-csv-in-python-pandas-comparing-2-csv%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
1
Could you please provide sample dataframes for
A
andB
and an expected output?– Scotty1-
Nov 15 '18 at 13:55