Michael Scofield
Michael Scofield

Reputation: 147

How to add ROW of one dataframe to column of another dataframe?

I've Data frame (df1):

                 DP1       DP2       DP3       DP4       DP5       DP6       DP7       DP8       DP9      DP10
OP1            3.359070  1.748457  1.488064  1.072251  1.209890  1.070304  1.060037  1.073360  1.018044   NaN
OP2            2.978232  1.879798  1.243196  1.187115  1.106631  1.153122  1.061442  1.058955       NaN   NaN
OP3            4.187777  1.723949  1.293835  1.187355  1.092288  1.086494  1.047356       NaN       NaN   NaN
OP4            3.957950  1.496112  1.458111  1.222045  1.013905  1.123005       NaN       NaN       NaN   NaN
OP5            3.609144  1.755843  1.295679  1.058462  1.002556       NaN       NaN       NaN       NaN   NaN
OP6            3.835973  2.056845  1.575320  1.092493       NaN       NaN       NaN       NaN       NaN   NaN
OP7            3.408095  1.865307  1.653651       NaN       NaN       NaN       NaN       NaN       NaN   NaN
OP8            4.590085  1.870876       NaN       NaN       NaN       NaN       NaN       NaN       NaN   NaN
OP9            4.690647       NaN       NaN       NaN       NaN       NaN       NaN       NaN       NaN   NaN
Weighted Avg   3.827345  1.798862  1.431810  1.144936  1.078714  1.110358  1.055943  1.065052  1.018044   1.0
CDF           15.477815  4.044008  2.248093  1.570105  1.371348  1.271280  1.144927  1.084270  1.018044   1.0

second Data Frame (df2):

           Paid
OP1   3901463.0
OP2   5339085.0
OP3   4909315.0
OP4   4588268.0
OP5   3873311.0
OP6   3691712.0
OP7   3483130.0
OP8   2864498.0
OP9   1363294.0
OP10   344014.0

I would like add last row of df1(CDF) to df2 as a column in reverse of order.

I want Output Like this:

  Paid        CDF
 3901463     1.00 
 5339085     1.01 
 4909315     1.08 
 4588268     1.14 
 3873311     1.27 
 3691712     1.37 
 3483130     1.57 
 2864498     2.24 
 1363294     4.04 
 344014      15.47 

Upvotes: 2

Views: 1343

Answers (3)

sophocles
sophocles

Reputation: 13841

A simple way would be:

df2['CDF'] = df.loc['CDF'].values[::-1]

Or with using pandas.DataFrame.assign

df2 = df2.assign(CDF=df.loc['CDF'].values[::-1])

Both print:

          Paid        CDF
index                    
OP1    3901463          1
OP2    5339085   1.018044
OP3    4909315   1.084270
OP4    4588268   1.144927
OP5    3873311   1.271280
OP6    3691712   1.371348
OP7    3483130   1.570105
OP8    2864498   2.248093
OP9    1363294   4.044008
OP10    344014  15.477815

Upvotes: 4

Chase
Chase

Reputation: 383

A little clunky, but if you want to keep this in all pandas you could do:

import pandas as pd

df2['CDF'] = pd.Series(df1.loc['CDF'].sort_values().values, index=df2.index)

Upvotes: 1

tofd
tofd

Reputation: 620

You can extract the values from the row, use np.flip() to reverse them and then create a new column.

import numpy as np

df2['CDF'] = np.flip(df1.loc['CDF'].values)

Upvotes: 2

Related Questions