pandas 将索引值相加的方法

yipeiwu_com6年前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设计】。

相关文章

把项目从Python2.x移植到Python3.x的经验总结

 经历移植jinja2到python3的痛苦之后,我把项目暂时放一放,因为我怕打破python3的兼容。我的做法是只用一个python2的代码库,然后在安装的时候用2to3工具...

对python中基于tcp协议的通信(数据传输)实例讲解

对python中基于tcp协议的通信(数据传输)实例讲解

阅读目录 tcp协议:流式协议(以数据流的形式通信传输)、安全协议(收发信息都需收到确认信息才能完成收发,是一种双向通道的通信) tcp协议在OSI七层协议中属于传输层,它上承用户层的数...

python 定时器,实现每天凌晨3点执行的方法

如下所示: ''' Created on 2018-4-20 例子:每天凌晨3点执行func方法 ''' import datetime import threading def...

Python使用gensim计算文档相似性

pre_file.py #-*-coding:utf-8-*- import MySQLdb import MySQLdb as mdb import os,sys,string i...

Python理解递归的方法总结

递归 一个函数在执行过程中一次或多次调用其本身便是递归,就像是俄罗斯套娃一样,一个娃娃里包含另一个娃娃。 递归其实是程序设计语言学习过程中很快就会接触到的东西,但有关递归的理解可能还会有...