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.
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With