0

I am looking to take two csvs (read in through pandas), and combine them into a single 3D DataFrame.

The formats are similar to this:

Table1:

key1 key2 key3 value
x1   y1   z1   1   
x1   y2   z1   2
x1   y3   z1   3
x2   y1   z1   4
x2   y2   z1   5
x2   y3   z2   6
x3   y1   z2   7
x3   y2   z2   8
x3   y3   z2   9

Table2:

key2 key3 value x1 x2 x3
y1   z1   0
y2   z1   1
y3   z1   2
y1   z2   3
y2   z2   4
y3   z2   5

My goal is that in table2 the values under the 'x' headers should be a lookup of the value in table1 (using all 3 keys) multiplied by the value in table2.

1 Answer 1

2

IIUC:

d1.set_index(
    ['key2', 'key3', 'key1']
).value.unstack().rename_axis(None, 1).reset_index()

  key2 key3   x1   x2   x3
0   y1   z1  1.0  4.0  NaN
1   y1   z2  NaN  NaN  7.0
2   y2   z1  2.0  5.0  NaN
3   y2   z2  NaN  NaN  8.0
4   y3   z1  3.0  NaN  NaN
5   y3   z2  NaN  6.0  9.0
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.