0

I hava a dataframe like this:

df1 = pd.DataFrame({'A': [1, 2, 3], 'B': ['a', 'b', 'c'], 'C': [4, 5, 6], 'D': ['e', 'f', 'g'], 'E': [7, 8, 9], id: [25, 15, 30]})

enter image description here

I would like to use the values of df1 (and their respective columns) as a basis for filling in df2.

Expected:

expected = pd.DataFrame({'column': ['A', 'B', 'C', 'D', 'E', 'A', 'B', 'C', 'D', 'E'], 'value': [1, 'a', 4, 'e', 7, 2, 'b', 5, 'f', 8], 'id': [25, 15]})

enter image description here

I tried using iterrows, but as I need to use it for a large amount of data, the performance results were not positive. Can you help me?

2 Answers2

1
df1 = pd.DataFrame({'A': [1, 2, 3], 'B': ['a', 'b', 'c'], 'C': [4, 5, 6], 'D': ['e', 'f', 'g'], 'E': [7, 8, 9], 'id': [25, 15, 30]})

pd.melt(df1, id_vars=['id'], var_name = 'column')
    id  column  value
0   25  A   1
1   15  A   2
2   30  A   3
3   25  B   a
4   15  B   b
5   30  B   c
6   25  C   4
7   15  C   5
8   30  C   6
9   25  D   e
10  15  D   f
11  30  D   g
12  25  E   7
13  15  E   8
14  30  E   9
Z Li
  • 4,133
  • 1
  • 4
  • 19
0

Have you tried Dataframe.melt? I guess something like this could do the trick:

df1.melt(ignore_index=False).merge(
    df1, left_index=True, right_index=True
)[['variable', 'value', 'id']].reset_index()

There are some rows to be ignored, but that should be easy. I don't now about performance regarding large data frames, though.