更新时间:2023-12-01 10:05:22
假设数据框的大小相同,则可以分配 RESULT_df ['RESULT']。values
到原始数据框。这样,您就不必担心索引问题。
Assuming the size of your dataframes are the same, you can assign the RESULT_df['RESULT'].values
to your original dataframe. This way, you don't have to worry about indexing issues.
# pre 0.24
feature_file_df['RESULT'] = RESULT_df['RESULT'].values
# >= 0.24
feature_file_df['RESULT'] = RESULT_df['RESULT'].to_numpy()
最小代码样本
df
A B
0 -1.202564 2.786483
1 0.180380 0.259736
2 -0.295206 1.175316
3 1.683482 0.927719
4 -0.199904 1.077655
df2
C
11 -0.140670
12 1.496007
13 0.263425
14 -0.557958
15 -0.018375
让我们先尝试直接分配。
Let's try direct assignment first.
df['C'] = df2['C']
df
A B C
0 -1.202564 2.786483 NaN
1 0.180380 0.259736 NaN
2 -0.295206 1.175316 NaN
3 1.683482 0.927719 NaN
4 -0.199904 1.077655 NaN
现在,a分配由 .values
返回的数组(或对于 .to_numpy()返回)。 .values
返回一个没有索引的 numpy
数组。
Now, assign the array returned by .values
(or .to_numpy()
for pandas versions >0.24). .values
returns a numpy
array which does not have an index.
df2['C'].values
array([-0.141, 1.496, 0.263, -0.558, -0.018])
df['C'] = df2['C'].values
df
A B C
0 -1.202564 2.786483 -0.140670
1 0.180380 0.259736 1.496007
2 -0.295206 1.175316 0.263425
3 1.683482 0.927719 -0.557958
4 -0.199904 1.077655 -0.018375