1

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.

1 Answer 1

1

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()
Sign up to request clarification or add additional context in comments.

3 Comments

Sorry, but i don't see how is this any different from the solution i proposed. Like i said, if i have a lot more data, this wouldn't be scalable. Ideally, i should only select the start and end indices i wish to replace from both L1 and L2. Something like, L2 start in day1000 and end in day1500 and L1 start in day100 and end in day600.
@Murilo answer was edited.
Great! Didn't knew about that to_numpy() thing when dealing with different indices.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.