Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas Dataframe - Set Rows Equal to Other Rows

For this basic dataframe, I'd like to set rows equal to other rows. I have no trouble doing this one row at a time:

import pandas as pd
import numpy as np

df = pd.DataFrame(np.random.randn(10, 5))
df.loc[6,:] = df.loc[4,:]

However, when I attempt to do this for multiple rows, they are set to NaN rather than the rows I'm referencing:

import pandas as pd
import numpy as np

df = pd.DataFrame(np.random.randn(10, 5))
df.loc[5:6,:] = df.loc[3:4,:]

I have read through the documentation and cannot find an explanation. Any ideas? Thank you.

like image 597
Jeff Coughlin Avatar asked Feb 14 '23 12:02

Jeff Coughlin


1 Answers

Pandas will always try to align data based on the index. When you assign one DataFrame to another, it tries to match the data up based on the index, not the sequential row number.

So when you assign to rows 5-6, it looks for rows in the assigned data with those index labels --- that is, it looks for rows whose indexes are 5 and 6. Since the passed-in data doesn't have any rows like that (since you passed in a slice containing rows 3 and 4 only), it doesn't find any matching data, so it puts in NaN.

To assign the "raw" values, disregarding indexes, you can do df.loc[5:6,:] = df.loc[3:4,:].values.

like image 60
BrenBarn Avatar answered Feb 16 '23 02:02

BrenBarn