Python 从具有不同索引的另一个数据帧在熊猫数据帧中添加一个新列
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/46396257/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Adding a new column in pandas dataframe from another dataframe with differing indices
提问by Jayashree
This is my original dataframe.
This is my second dataframe containing one column.
I want to add the column of second dataframe to the original dataframe at the end.Indices are different for both dataframes.
I did like this
这是我的原始数据框。
这是我包含一列的第二个数据框。
我想在最后将第二个数据帧的列添加到原始数据帧中。两个数据帧的索引不同。我喜欢这个
feature_file_df['RESULT']=RESULT_df['RESULT']
Result column got added but all values are NaN's
How to add columns with value
如何添加带有值的列
回答by cs95
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.
假设您的数据帧的大小相同,您可以将 分配RESULT_df['RESULT'].values
给您的原始数据帧。这样,您就不必担心索引问题。
# pre 0.24
feature_file_df['RESULT'] = RESULT_df['RESULT'].values
# >= 0.24
feature_file_df['RESULT'] = RESULT_df['RESULT'].to_numpy()
Minimal Code Sample
最少的代码示例
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
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.
现在,分配由.values
(或.to_numpy()
熊猫版本> 0.24)返回的数组。.values
返回一个numpy
没有索引的数组。
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