0
  1. list of dataframes (ex. dfs = [df1, df2, ...])

  2. all dataframe columns is multiindex (ex. [("something", "id",), ("something", "age"), ...]) enter image description here

  3. i want to set "concept_id" to Key and create a new dataframe like this: enter image description here

i try using python pandas, but it was hard. (multiindex is really hard) what should i do?

  1. pd.concat(dfs, keys=["concept_id"], join="outer", axis=1)

It didn't come close to the correct answer.

  1. reduce(lambda df1, df2: pd.merge(df1, df2, on="concept_id", how="outer"), dfs)

raised error : xlsxwriter.exceptions.OverlappingRange: Merge range 'B1:J1' overlaps previous merge range 'B1:J1'.

1

1 Answer 1

0

IIUC, you need to set all "something" columns as index during the concat (if you want a unique set of those 3 columns, one can assume that they are all common), then post-process the output to reorganize the levels (e.g. with reorder_levels):

idx = pd.MultiIndex.from_tuples([('something', 'id'), ('something', 'age'), ('something', 'label_en'),
                                 ('2023', '01'), ('2023', '02')])
df1 = pd.DataFrame(1, columns=idx, index=[0])
df2 = pd.DataFrame(1, columns=idx, index=[0])

dfs = [df1, df2]

cols = [('something', 'id'), ('something', 'age'), ('something', 'label_en')]

out = (pd.concat([d.set_index(cols) for d in dfs],
                 keys=['df1', 'df2'], join='outer', axis=1)
         .reorder_levels([1,2,0], axis=1)
         .reset_index()
         .reorder_levels([2,0,1], axis=1)
      )

Output:

                          df1     df2   
  something              2023    2023   
         id age label_en   01 02   01 02
0         1   1        1    1  1    1  1
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.