0

I have a dataframe which looks like this

df = pd.DataFrame({
 'price': {0: 639.0, 1: 639.0, 2: 849.0, 3: 849.0, 4: 128.0},
 'special_price': {0: '599',
  1: '599',
  2: '849',
  3: '849',
  4: 'data not available'}})

i would like to search the dataframe and replace any value of 'data not available' in column['special_price'] with the corresponding from the column['price']

the result should look like

df = pd.DataFrame({
 'price': {0: 639.0, 1: 639.0, 2: 849.0, 3: 849.0, 4: 128.0},
 'special_price': {0: '599',
  1: '599',
  2: '849',
  3: '849',
  4: '128'}})

What is the fastest way to do that

I have tried

def replaceDaWithNil(row):
        if row['special_price'] == 'data not available':
            row['special_price'] = row['price']
        return row

df[['price','special_price']].apply(replaceDaWithNil, axis = 1)

This takes a very long time, so im wondering if there is a faster way

Nic Wanavit
  • 2,363
  • 5
  • 19
  • 31
  • This is not the same as the question referenced, basically i want to replace the value with data from another column, not with a fixed value – Nic Wanavit Apr 02 '20 at 05:49
  • Have you tried using np.where? Similar to https://stackoverflow.com/questions/39903090/efficiently-replace-values-from-a-column-to-another-column-pandas-dataframe – Asad Rauf Apr 02 '20 at 06:13

0 Answers0