0

This is my current dataframe. I would like to add two new columns called PriceError and CostError.

Price 1  Price2 Cost1 Cost2   %Price %Cost PriceError  CostError
1        1      3    6        0     100                      
2        4      3    3        100   0           

df['PriceError'] should equal the following string Price1 is 2 and Price1 is 4. The %Price is 100.

df['CostError'] should equal the following string Cost1 is 3 and Cost1 is 6. The %Price is 100.

I am only returning those because the other two are below 50% which is my threshold for errors.

SO PriceError is hardcoded to "Price1 is df['Price1'] and Price2 is df['Price2'] and the %Price is df['%Price']".

I would like to some sort of way to write that PriceError should only look at columns price1, price2, and %price and write the string.

Fred
  • 1,462
  • 8
  • 15
  • 2
    I'm having trouble understanding your question. Can you try and format your data as described in [How to make good reproducible pandas examples](https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples). In addition, please can you explain the calculation explicitly (not in the dataframe itself). – jpp May 03 '18 at 13:43

1 Answers1

0

Is this what you aimed for:

df['PriceError'] = df.filter(like='Price').apply(lambda x: 'Price1 is {} and Price2 is {}. The %Price is {}'.format(*x), axis = 1)

And to avoid hardcoding this is the workaround:

prices = df.filter(like='Price')

df['PriceError'] = prices.apply(lambda x: '{3} is {0} and {4} is {1}. The {5} is {2}.'.format(*(list(x)+prices.columns.tolist())), axis=1)
zipa
  • 27,316
  • 6
  • 40
  • 58
  • it is closer. But after the the filter, is there a way to reference the columns headers instead of hardcoding Price1 and Price2? –  May 03 '18 at 14:46
  • I made it, but I don't like it :) – zipa May 03 '18 at 15:05