Home > Enterprise >  Replace multiple values based on index in a dataframe
Replace multiple values based on index in a dataframe

Time:08-19

We have the following dataframe:

import pandas as pd
df_test = pd.DataFrame(data=[10, 20, 30, 40, 50, 60, 70, 80, 90, 100],
                       index=['day1', 'day2', 'day3', 'day4', 'day5', 'day6', 'day7', 'day8', 'day9', 'day10'])

How can i replace multiple values in it based on index? For example, i wish that 'day9' and 'day10' rows receive the values of 'day1' and 'day2'.

I know we can do something like:

df_test.loc['day9'] = df_test.loc['day1']
df_test.loc['day10'] = df_test.loc['day2']

But if i have more data to replace, this wouldn't scale well. Not really sure how can i automate this process.

CodePudding user response:

Create lists of indices and then replace in DataFrame.loc, because different indices is necessary convert values to list or numpy array:

L1 = ['day1','day2']
L2 = ['day9','day10']

df_test.loc[L2] = df_test.loc[L1].to_numpy()
print (df_test)
        0
day1   10
day2   20
day3   30
day4   40
day5   50
day6   60
day7   70
day8   80
day9   10
day10  20

Another idea with rename for new indice with DataFrame.update:

L1 = ['day1','day2']
L2 = ['day9','day10']

df_test.update(df_test.loc[L1].rename(dict(zip(L1, L2))))
print (df_test)
          0
day1   10.0
day2   20.0
day3   30.0
day4   40.0
day5   50.0
day6   60.0
day7   70.0
day8   80.0
day9   10.0
day10  20.0

EDIT: If need replace values by range:

df_test.loc['day8':'day10'] = df_test.loc['day1':'day3'].to_numpy()
  • Related