在 Pandas 中将两个系列组合成一个 DataFrame
为了在Pandas中将两个系列组合成一个DataFrame,我们可以取两个系列并使用concat()方法将它们连接起来。
步骤
创建具有两个元素的系列1,其中索引是['a','b']并且名称是系列1。
打印系列1。
使Series2具有两个元素,其中index为['a','b']并且name为Series2。
打印系列2。
沿特定轴连接Pandas对象,沿其他轴使用可选的设置逻辑。
打印结果数据帧。
示例
import pandas as pd s1 = pd.Series([4, 16], index=['a', 'b'], name='Series 1') print "Input series 1 is: \n", s1 s2 = pd.Series([3, 9], index=['a', 'b'], name='Series 2') print "Input series 2 is: \n", s2 df = pd.concat([s1, s2], axis=1) print "Resultant DataFrame is:\n", df输出结果
Input series 1 is: a 4 b 16 Name: Series 1, dtype: int64 Input series 2 is: a 3 b 9 Name: Series 2, dtype: int64 Resultant DataFrame is: Series 1 Series 2 a 4 3 b 16 9