如何使用一些新索引将新列添加到 Pandas DataFrame(来自系列)?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/44098934/
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
How to add new column to pandas DataFrame (from series) with some new indices?
提问by MarkD
Lets say I have the following:
可以说我有以下几点:
df = pd.DataFrame(data=[[0.5, 0.2],[0.5, 0.8]], columns=['Col1', 'Col2'], index=['Existing 1', 'Existing 2'])
new_col = pd.Series(data=[0.6, 0.4], index=['Existing 1', 'New 1'])
Which yields:
其中产生:
df:
Col1 Col2
Existing 1 0.5 0.2
Existing 2 0.5 0.8
new_col:
Existing 1 0.6
New 1 0.4
What I would like to do is add new_col under a column called 'New', adding the "New 1" index, and filling empty with nan. I have tried via:
我想要做的是在名为“New”的列下添加 new_col,添加“New 1”索引,并用 nan 填充空。我尝试过:
df['New'] = new_col
However this does not seem to append the "New 1" index. Thus I end up with:
然而,这似乎没有附加“New 1”索引。因此我最终得到:
Col1 Col2 New
Existing 1 0.5 0.2 0.6
Existing 2 0.5 0.8 NaN
Where I want:
我想要的地方:
Col1 Col2 New
Existing 1 0.5 0.2 0.6
Existing 2 0.5 0.8 NaN
New 1 NaN NaN 0.4
Thoughts?
想法?
回答by Psidom
You can use pd.concat
to concatenate the series with the data frame along axis=1
, which by default does an outer
join, and thus inluding the index from both the data frame and Series in the result:
您可以使用pd.concat
将系列与数据框沿连接起来,axis=1
默认情况下会进行outer
连接,从而在结果中包含来自数据框和系列的索引:
pd.concat([df, new_col.rename('New')], axis=1)
# Col1 Col2 New
#Existing 1 0.5 0.2 0.6
#Existing 2 0.5 0.8 NaN
# New 1 NaN NaN 0.4