pandas 将索引值相加的方法

yipeiwu_com5年前Python基础

如下所示:

 s1 = pd.Series([1, 2, 3, 4], index=['a', 'b', 'c', 'd'])
 s2 = pd.Series([10, 20, 30, 40], index=['a', 'b', 'c', 'd'])
 print s1 + s2
a 11
b 22
c 33
d 44
dtype: int64
 s1 = pd.Series([1, 2, 3, 4], index=['a', 'b', 'c', 'd'])
 s2 = pd.Series([10, 20, 30, 40], index=['b', 'd', 'a', 'c'])
 print s1 + s2
a 31
b 12
c 43
d 24
dtype: int64
 s1 = pd.Series([1, 2, 3, 4], index=['a', 'b', 'c', 'd'])
 s2 = pd.Series([10, 20, 30, 40], index=['c', 'd', 'e', 'f'])
 print s1 + s2
a  NaN
b  NaN
c 13.0
d 24.0
e  NaN
f  NaN
dtype: float64
 s1 = pd.Series([1, 2, 3, 4], index=['a', 'b', 'c', 'd'])
 s2 = pd.Series([10, 20, 30, 40], index=['e', 'f', 'g', 'h'])
 print s1 + s2
a NaN
b NaN
c NaN
d NaN
e NaN
f NaN
g NaN
h NaN
dtype: float64

以上这篇pandas 将索引值相加的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

初步讲解Python中的元组概念

初步讲解Python中的元组概念

 元组是不可变的Python对象序列。元组的序列就像列表。唯一的区别是,元组不能被改变,即元组是不可被修改。元组使用小括号,而列表使用方括号。 创建一个元组很简单,只要把不同的...

基于python实现把图片转换成素描

基于python实现把图片转换成素描

这篇文章主要介绍了基于python实现把图片转换成素描,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 导语: 你是否还在为当时年少时没...

Python3中urlencode和urldecode的用法详解

在Python3中,将中文进行urlencode编码使用函数 urllib.parse.quote(string, safe='/', encoding=None, errors=N...

python 判断三个数字中的最大值实例代码

python 判断三个数字中的最大值,具体代码如下所示: #判断三个数中最大值 n1= int(input('please enter the firest number:')) n...

python 模拟银行转账功能过程详解

python 模拟银行转账功能过程详解

首先画出流程图,流程图与现实代码有出入,因为刚开始画流程图的时候,有些东西没考虑进去,后来写着写着就慢慢能想起来并实现了。 另有一点经验推荐给新手朋友,如果说碰到一个项目无从下手的话,...