2

I have foll. dataframes (df_A and df_B):

df_A
Col_A     Col_B
23         34
12         18

df_B
23          12
45          10

I want to merge them based on Col_A in df_A and 1st column in df_B. Note that df_B does not have any header row. How do I do this? A simple merge would have been:

df_A.merge(df_B, on='Col_A')

However that does not work in this case becuase df_B does not have Col_A as column.

--EDIT based on @EdChum's suggestion:

TO CLARIFY: I want to merge two dataframes. One has a header, and the other one does not. How do i specify the join column on a dataframe that does not have a header? Can I specify it by location/index?

df_B.info()
<class 'pandas.core.frame.DataFrame'>
Int64Index: 1 entries, 0 to 0
Data columns (total 2 columns):
23    1 non-null int64
12    1 non-null int64
dtypes: int64(2)
memory usage: 24.0 bytes

df_B.columns.tolist()
['23', '12']


df_A.info()
<class 'pandas.core.frame.DataFrame'>
Int64Index: 2 entries, 0 to 1
Data columns (total 2 columns):
Col_A    2 non-null int64
Col_B    2 non-null int64
dtypes: int64(2)
memory usage: 48.0 bytes

df_A.columns.tolist()
['Col_A', 'Col_B']
10
  • Post the output from df_A.info() and df_B.info() and also the output from df_A.columns.tolist() and the same for df_B they will have columns despite what you think Commented Nov 10, 2015 at 15:09
  • 2
    Well it looks like you want to merge like so: df_A.merge(df_B, left_on='Col_A', right_on='23') Commented Nov 10, 2015 at 15:15
  • 2
    Not according to df_B.info(), what does df_B.columns.tolist() show Commented Nov 10, 2015 at 15:17
  • 1
    Well you've answered my question then, you've either imported the file incorrectly or you'll have to merge using my code Commented Nov 10, 2015 at 15:19
  • 1
    Nope, you have to fix df_B to get what you want Commented Nov 10, 2015 at 15:24

1 Answer 1

1

You can set column names by df_B.columns = ['Col_B', 'Col_A']. Then you can merge dataframes.

print df_A
   Col_A  Col_B
0     23     34
1     12     18

print df_B
    0   1
0  23  12
1  45  10

df_B.columns = ['Col_A', 'Col_B']
print df_B
   Col_A  Col_B
0     23     12
1     45     10

out = df_A.merge(df_B, on='Col_A')
print out
   Col_A  Col_B_x  Col_B_y
0     23       34       12
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.