Home > Mobile >  Looping through pandas and finding row column pairs
Looping through pandas and finding row column pairs

Time:11-13

I can't wrap my head around the best way to accomplish this.

The visualization below is what I would like to accomplish. I'm not sure what you would call it exactly but essentially I want to iterate through rows and columns and make a new dataframe with the x, y and then the intercepting point. Just reshaping the dataframe I don't want to lose any values.

I'm doing this just to learn Pandas so any help in the right direction of how to think about this/ best way to solve it would be greatly appreciated.

   1  2  3  

1  10 15 20

2  11 16 21

3  12 17 22
   x  y  z

   1  1  10

   1  2  15

   1  3  20
   
   2  1  11
   
   2  2  16

   2  3  21
 
   3  1  12

   3  2  17

   3  3  22

CodePudding user response:

Given your dataset and expected output you are looking for pandas melt. Iterating over a dataframe can be slow and inefficient, if you are learning I strongly suggest you look for more efficient ways of working (for example vectorizing an operation, or pivoting) instead of using for loops. The last line of the proposed solution is merely for ordering the columns to match your desired output. Kindly try the following:

df = pd.DataFrame({1:[10,11,12],
                   2:[15,16,17],
                   3:[20,21,22]})
df = df.melt(value_vars=[1,2,3],var_name='x',value_name='z')
df['y'] = df.groupby('x').cumcount() 1
df[['x','y','z']]

Outputs:

   x  y   z
0  1  1  10
1  1  2  11
2  1  3  12
3  2  1  15
4  2  2  16
5  2  3  17
6  3  1  20
7  3  2  21
8  3  3  22
  • Related