Pandas的对齐运算

是数据清洗的重要过程,可以按索引对齐进行运算,如果没对齐的位置则补NaN,最后也可以填充NaN

Series的对齐运算

1. Series 按行、索引对齐

示例代码:

s1 = pd.Series(range(10, 20), index = range(10))
s2 = pd.Series(range(20, 25), index = range(5)) print('s1: ' )
print(s1) print('') print('s2: ')
print(s2)

运行结果:

s1:
0 10
1 11
2 12
3 13
4 14
5 15
6 16
7 17
8 18
9 19
dtype: int64 s2:
0 20
1 21
2 22
3 23
4 24
dtype: int64

2. Series的对齐运算

示例代码:

# Series 对齐运算
s1 + s2

运行结果:

0    30.0
1 32.0
2 34.0
3 36.0
4 38.0
5 NaN
6 NaN
7 NaN
8 NaN
9 NaN
dtype: float64

DataFrame的对齐运算

1. DataFrame按行、列索引对齐

示例代码:

df1 = pd.DataFrame(np.ones((2,2)), columns = ['a', 'b'])
df2 = pd.DataFrame(np.ones((3,3)), columns = ['a', 'b', 'c']) print('df1: ')
print(df1) print('')
print('df2: ')
print(df2)

运行结果:

df1:
a b
0 1.0 1.0
1 1.0 1.0 df2:
a b c
0 1.0 1.0 1.0
1 1.0 1.0 1.0
2 1.0 1.0 1.0

2. DataFrame的对齐运算

示例代码:

# DataFrame对齐操作
df1 + df2

运行结果:

     a    b   c
0 2.0 2.0 NaN
1 2.0 2.0 NaN
2 NaN NaN NaN

填充未对齐的数据进行运算

1. fill_value

使用addsubdivmul的同时,

通过fill_value指定填充值,未对齐的数据将和填充值做运算

示例代码:

print(s1)
print(s2)
s1.add(s2, fill_value = -1) print(df1)
print(df2)
df1.sub(df2, fill_value = 2.)

运行结果:

# print(s1)
0 10
1 11
2 12
3 13
4 14
5 15
6 16
7 17
8 18
9 19
dtype: int64 # print(s2)
0 20
1 21
2 22
3 23
4 24
dtype: int64 # s1.add(s2, fill_value = -1)
0 30.0
1 32.0
2 34.0
3 36.0
4 38.0
5 14.0
6 15.0
7 16.0
8 17.0
9 18.0
dtype: float64 # print(df1)
a b
0 1.0 1.0
1 1.0 1.0 # print(df2)
a b c
0 1.0 1.0 1.0
1 1.0 1.0 1.0
2 1.0 1.0 1.0 # df1.sub(df2, fill_value = 2.)
a b c
0 0.0 0.0 1.0
1 0.0 0.0 1.0
2 1.0 1.0 1.0

pandas数据对齐的更多相关文章

  1. pandas读书笔记 算数运算和数据对齐

    pandas最重要的一个功能是,它可以对不同索引的对象进行算数运算.在对象相加时,如果存在不同的索引对,则结果的索引就是该索引对的并集. Series s1=Series([,3.4,1.5],ind ...

  2. pandas数据操作

    pandas数据操作 字符串方法 Series对象在其str属性中配备了一组字符串处理方法,可以很容易的应用到数组中的每个元素 t = pd.Series(['a_b_c_d','c_d_e',np. ...

  3. C++中数据对齐

    大体看了看数据对齐,不知道是否正确,总结如下: struct A { char name; double dHeight; int age; }; sizeof(A) = (1+7+8+4+4) =  ...

  4. C/C++数据对齐汇总

     C/C++数据对齐汇总  这里用两句话总结数据对齐的原则: (1)对于n字节的元素(n=2,4,8,...),它的首地址能被n整除,才干获得最好的性能: (2)如果len为结构体中长度最长的变量,s ...

  5. gpu显存(全局内存)在使用时数据对齐的问题

    全局存储器,即普通的显存,整个网格中的随意线程都能读写全局存储器的任何位置. 存取延时为400-600 clock cycles  很easy成为性能瓶颈. 訪问显存时,读取和存储必须对齐,宽度为4B ...

  6. 数据分析与展示——Pandas数据特征分析

    Pandas数据特征分析 数据的排序 将一组数据通过摘要(有损地提取数据特征的过程)的方式,可以获得基本统计(含排序).分布/累计统计.数据特征(相关性.周期性等).数据挖掘(形成知识). .sort ...

  7. pandas小记:pandas数据输入输出

    http://blog.csdn.net/pipisorry/article/details/52208727 数据输入输出 数据pickling pandas数据pickling比保存和读取csv文 ...

  8. 结构体的数据对齐 #pragma浅谈

    之前若是有人拿个结构体或者联合体问我这个结构占用了多少字节的内存,我一定觉得这个人有点low, 直到某某公司的一个实习招聘模拟题的出现,让我不得不重新审视这个问题, 该问题大致如下: typedef ...

  9. Pandas数据排序

    Pandas数据排序 .sort_index() 在指定轴上根据索引进行排序,索引排序后内容会跟随排序 b = pd.DataFrame(np.arange(20).reshape(4,5),inde ...

随机推荐

  1. day5-configparser模块

    一.概述 在软件开发过程中,很多时候需要处理配置文件的读取解析和改写,在python中专门处理配置文件的模块就是configpaser了.顾名思义,configpaser就是配置解析器,可用来对符合格 ...

  2. 来自lombok的注解(解决idea中的找不到get,set方法,找不到log的问题)

    今天看代码,发现idea报错,仔细一看调用的get,set方法bean中都没有,但是运行起来却没有问题,这个让我很疑惑.后来发现在类上有一个以前没见过的注解@Data,大概就是因为有他的原因.这个注解 ...

  3. 【spark】分区

    RDD是弹性分布式数据集,通常RDD很大,会被分成多个分区,保存在不同节点上. 那么分区有什么好处呢? 分区能减少节点之间的通信开销,正确的分区能大大加快程序的执行速度. 我们看个例子 首先我们要了解 ...

  4. Oracle11g数据库监听配置

    (转自:http://blog.sina.com.cn/s/blog_6908928501018057.html) 经验告诉我:最好把数据库的SID和数据库全局名称分开,免得配置时混了,如果要配置服务 ...

  5. Qt类型转换

    (转自:http://qimo601.iteye.com/blog/1260479) 1.char * 与 const char *的转换 char *ch1="hello11"; ...

  6. 【lightoj-1039】A Toy Company(BFS)

    The toy company "Babies Toys" has hired you to help develop educational toys. The current ...

  7. List排序共通代码

    此共通方法可以根据特定字段进行排序 package com.gomecar.index.common.utils; import java.lang.reflect.Method; import ja ...

  8. vsftp中的local_umask和anon_umask

    umask是unix操作系统的概念,umask决定目录和文件被创建时得到的初始权限umask = 022 时,新建的目录 权限是755,文件的权限是 644umask = 077 时,新建的目录 权限 ...

  9. ng 实现插入和删除

    结果: 代码: <!DOCTYPE html> <html ng-app="myApp"> <head lang="en"> ...

  10. RAD Studio Mobile Roadmap updated,XE5 will released on next month, Andriod will be supported.

    RAD Studio Mobile Roadmap updated   Embarcadero updated his RAD Studio Mobile Roadmap. This concern ...