0

[1]: https://i.stack.imgur.com/ESmJ4.png

I want to create new column based on formula on values of other columns. Col-C is the most important and Col-A is the least. Need a formula in excel or pandas to get result as in image. Any help is much appreciated.

  • 1
    Welcome to StackOverflow. Please take the time to read this post on [how to provide a great pandas example](http://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples) as well as how to provide a [minimal, complete, and verifiable example](http://stackoverflow.com/help/mcve) and revise your question accordingly. These tips on [how to ask a good question](http://stackoverflow.com/help/how-to-ask) may also be useful. – jezrael Jul 24 '18 at 07:27

3 Answers3

1

First forward fill all mising values and then select last column by position by iloc:

df = pd.read_excel('file.xlsx')
df['Result']= df[['ColA','ColB','Colc']].ffill(axis=1).iloc[:, -1]
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
0

This formula checks whether the cell in the column is empty one by one, and returns the value of the first non-blank cell from C to A. If it finds no data, the cell remains empty.

=IF(ISBLANK(C1);IF(ISBLANK(B1);IF(ISBLANK(A1);"";A1);B1);C1)
M.Douda
  • 564
  • 1
  • 7
  • 18
0

Maybe it's not too elegant, but it works - you fill null values with each column's values, one by one, starting with the most important.

import pandas as pd

df = pd.read_excel('....xlsx')
df['Result']=df['Result'].fillna(df['ColC'])
df['Result']=df['Result'].fillna(df['ColB'])
df['Result']=df['Result'].fillna(df['ColA'])
Piotrek
  • 1,400
  • 9
  • 16