在 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热门推荐
10 香港老妈结婚祝福语简短
11 毕业立体贺卡祝福语简短
12 简短新年年会祝福语
13 评论小品祝福语大全简短
14 恭喜师兄结婚祝福语简短
15 员工集体辞职祝福语简短
16 高中新生祝福语 简短
17 装修祝福语男生搞笑简短
18 生日开业蛋糕祝福语简短