0

I have two dataframes-

cols = ['A','B']
data = [[-1,2],[0,2],[5,1]]
data = np.asarray(data)
indices = np.arange(0,len(data))

df = pd.DataFrame(data, index=indices, columns=cols)

cols = ['A','B']
data2 = [[-13,2],[-1,2],[0,4],[2,1],[5,0]]
data2 = np.asarray(data2)
indices = np.arange(0,len(data2))

df2 = pd.DataFrame(data2, index=indices, columns=cols)

Now I want to create a new dataframe which has for the same A the maximum of B from either dataframe.

Therefore, the output would be-

    A   B
0  -13  2
1  -1   2
2   0   4
3   2   1
4   5   1
3
  • 1
    Have you run into issues? What have you tried so far? Commented Oct 22, 2018 at 23:49
  • df1 has 3 rows, df2 has 5. When you say "for the same A the maximum of B from either dataframe" you mean "maximum of B for that specific row in either df". Commented Oct 22, 2018 at 23:56
  • 2
    Btw, you can just instantiate each dataframe in one line: df = pd.DataFrame([[-1,2],[0,2],[5,1]], columns=['A','B']). You don't need to cast data = np.asarray(data), and you don't need to generate and pass in the indices, it defaults to range-indices like you want. Makes your example much clearer. Commented Oct 22, 2018 at 23:59

2 Answers 2

4

Using drop_duplicates

pd.concat([df2,df]).sort_values('B').drop_duplicates('A',keep='last')
Out[80]: 
    A  B
3   2  1
2   5  1
0 -13  2
0  -1  2
2   0  4
Sign up to request clarification or add additional context in comments.

1 Comment

For completeness / efficiency, we probably need reset_index to get rid of the duplicate indices.
2

You can align indices, concatenate and then take the maximum:

res = pd.concat([df.set_index('A'), df2.set_index('A')], axis=1)\
        .max(1).astype(int).rename('B').reset_index()

print(res)

    A  B
0 -13  2
1  -1  2
2   0  4
3   2  1
4   5  1

2 Comments

Could you explain the part after concat?
Sure, max(1) takes row-wise maximums, astype(int) converts to integers (otherwise you get float output). Since the resultant series has name 0 we need to rename as 'B'. Finally, to elevate index to column we use reset_index.

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.