Generate new column based on values in another column and their index

Submitted 3 years, 7 months ago
Ticket #136
Views 217
Language/Framework Python
Priority Medium
Status Closed

In the df underneath, I want to sort the values of column 'cdf_X' based on column 'A' and 'X'. Column 'X' and 'cdf_X' are connected, so if a value in 'X' appears in column 'A', the value of 'cdf_X' should be repositioned to that index number of column 'A' in a new column. (Values don't occur twice in a column 'cdf_A'.)

Example: 'X'=3 at index 0 -> cdf_X=0.05 at index 0 -> '3' appears in column 'A' at index 4 -> cdf_A at index 4 = cdf_X at index 0

Initial df:

    A   X   cdf
0   7   3  0.05
1   4   4  0.15
2  11   7  0.27
3   9   9  0.45
4   3  11  0.69
5  13  13  1.00

Desired df:

   A   X   cdf_X   cdf_A      
0   7   3    0.05    0.27     
1   4   4    0.15    0.15
2  11   7    0.27    0.69
3   9   9    0.45    0.45
4   3  11    0.69    0.05
5  13  13    1.00    1.00

Tried code:

import pandas as pd

df = pd.DataFrame({"A": [7,4,11,9,3,13],

                 "cdf": [0.05,0.15,0.27,0.45,0.69,1.00],

                   "X": [3,4,7,9,11,13]})

-> I'm still figuring out how to do it

Submitted on Sep 10, 20
add a comment

1 Answer

Verified

I think you need replace

df['cdf_A'] = df.A.replace(df.set_index('X').cdf)

Out[989]:
    A   X   cdf  cdf_A
0   7   3  0.05   0.27
1   4   4  0.15   0.15
2  11   7  0.27   0.69
3   9   9  0.45   0.45
4   3  11  0.69   0.05
5  13  13  1.00   1.00

Submitted 3 years, 6 months ago


Latest Blogs