Skip to content Skip to sidebar Skip to footer

Create A Column That's A Combination Of Other Columns

I have a pandas dataframe with 3 columns, and I want to create a new column as follows: import pandas as pd import numpy as np np.random.seed(2019) df = pd.DataFrame(np.random.rand

Solution 1:

Use DataFrame.lookup:

print (df)
          A         B         C
0 -0.217679  0.821455  1.481278
1  1.331864 -0.361865  0.685609
2  0.573761  0.287728 -0.235634
3  0.953490 -1.689625 -0.344943
4  0.016905 -0.514984  0.244509

df['D'] = df.lookup(df.index, cols)
print (df)
          A         B         C         D
0 -0.217679  0.821455  1.481278  1.481278
1  1.331864 -0.361865  0.685609  1.331864
2  0.573761  0.287728 -0.235634  0.573761
3  0.953490 -1.689625 -0.344943 -1.689625
4  0.016905 -0.514984  0.244509 -0.514984

Post a Comment for "Create A Column That's A Combination Of Other Columns"