Having:
np.random.seed(42)
df_one = pd.DataFrame(np.random.rand(4,3), columns=['colA', 'colB', 'colC'])
And:
df_two = pd.DataFrame(np.ones([2,3]), columns=['colA', 'colB', 'colC'])
When I try to assign, like this:
df_one.loc[2:4, 'colB'] = df_two.loc[:, 'colB']
I get:
colA colB colC
0.37 0.95 0.73
0.59 0.15 0.15
0.05 NaN 0.60
0.70 NaN 0.96
When expecting to get:
colA colB colC
0.37 0.95 0.73
0.59 0.15 0.15
0.05 1.0 0.60
0.70 1.0 0.96
This can be done adding a to_list() at the end:
df_one.loc[2:4, 'colB'] = df_two.loc[:, 'colB'].to_list()
Or:
df_one.loc[2:4, 'colB'] = df_two.loc[:, 'colB'].values
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.