作为新列附加到 Pandas 中的 DataFrame
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31862644/
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
Append to a DataFrame in Pandas as new column
提问by TheStrangeQuark
I have two DataFrames with the same indexing and want to append the second to the first. Lets say I have:
我有两个具有相同索引的 DataFrame,并希望将第二个附加到第一个。可以说我有:
df1 = pd.DataFrame([1,2,3], index = [2,3,4])
df2 = pd.DataFrame([3,5,3], index = [2,3,4])
df1 = df1.append(df2)
which returns
返回
0
2 1
3 2
4 3
2 3
3 5
4 3
But I want it to append a new column where the indexes match:
但我希望它附加一个索引匹配的新列:
2 1 3
3 2 5
4 3 3
Is there a way to do this?
有没有办法做到这一点?
采纳答案by EdChum
Use concatand pass param axis=1to concatenate the list of dfs column-wise:
使用concat并传递 paramaxis=1来按列连接 dfs 列表:
In [3]:
df1 = pd.DataFrame([1,2,3], index = [2,3,4])
df2 = pd.DataFrame([3,5,3], index = [2,3,4])
pd.concat([df1,df2], axis=1)
Out[3]:
0 0
2 1 3
3 2 5
4 3 3
You can also use joinbut you have to rename the column first:
您也可以使用,join但您必须先重命名该列:
In [6]:
df1.join(df2.rename(columns={0:'x'}))
Out[6]:
0 x
2 1 3
3 2 5
4 3 3
Or mergespecifying that you wish to match on indices:
或者merge指定您希望匹配索引:
In [8]:
df1.merge(df2.rename(columns={0:'x'}), left_index=True, right_index=True )
Out[8]:
0 x
2 1 3
3 2 5
4 3 3
回答by vk1011
If the indexes match exactly and there's only one column in the other DataFrame (like your question has), then you could even just add the other DataFrame as a new column.
如果索引完全匹配并且另一个 DataFrame 中只有一列(就像您的问题一样),那么您甚至可以将另一个 DataFrame 添加为新列。
>>> df1['new_column'] = df2
>>> df1
0 new_column
2 1 3
3 2 5
4 3 3
In general, the concatapproach is better. If you have different indexes, you can choose to do an inner joinor outer join.
一般来说,这种concat方法更好。如果你有不同的索引,你可以选择做一个inner join或outer join。
>>> df2 = pd.DataFrame([3,5,3], index = [2,3,5])
>>> df2
0
2 3
3 5
5 3
>>> pd.concat([df1, df2], axis=1, join='inner')
0 0
2 1 3
3 2 5
>>> pd.concat([df1, df2], axis=1, join='outer')
0 0
2 1 3
3 2 5
4 3 NaN
5 NaN 3

