pandas的重新索引,算数运算和数据对齐

pip install pandas
Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple/Note: you may need to restart the kernel to use updated packages.
Requirement already satisfied: pandas in c:\users\zcc\appdata\local\programs\python\python37\lib\site-packages (1.0.3)
Requirement already satisfied: numpy>=1.13.3 in c:\users\zcc\appdata\local\programs\python\python37\lib\site-packages (from pandas) (1.18.2)
Requirement already satisfied: pytz>=2017.2 in c:\users\zcc\appdata\local\programs\python\python37\lib\site-packages (from pandas) (2019.3)
Requirement already satisfied: python-dateutil>=2.6.1 in c:\users\zcc\appdata\local\programs\python\python37\lib\site-packages (from pandas) (2.8.1)

Requirement already satisfied: six>=1.5 in c:\users\zcc\appdata\local\programs\python\python37\lib\site-packages (from python-dateutil>=2.6.1->pandas) (1.14.0)


WARNING: You are using pip version 20.0.2; however, version 20.1.1 is available.
You should consider upgrading via the 'c:\users\zcc\appdata\local\programs\python\python37\python.exe -m pip install --upgrade pip' command.
import pandas as pd
from pandas import Series,DataFrame

pandas的重新索引reindex

obj = Series([4,5,6],index = ['a','b','c'])
print(obj)
obj_1 = obj.reindex(['a','b','s','d','r'])
print(obj_1)
a    4
b    5
c    6
dtype: int64
a    4.0
b    5.0
s    NaN
d    NaN
r    NaN
dtype: float64
obj_2 = obj.reindex(['a','b','s','d','f'])
print(obj_2)
obj_2 = obj.reindex(['a','b','s','d','f'],fill_value=1)
print(obj_2)
a    4.0
b    5.0
s    NaN
d    NaN
f    NaN
dtype: float64
a    4
b    5
s    1
d    1
f    1
dtype: int64
obj = Series([4,5,6,7],index = ['a','b','c','g'])
print(obj)
obj_3 = obj.reindex(['a','b','c','d','e'])
print(obj_3)
obj_3 = obj.reindex(['a','b','c','d','e'],method='pad')#前向值填充
print(obj_3)
obj_3 = obj.reindex(['a','b','c','d ','e'],method='backfill')
print(obj_3)
a    4
b    5
c    6
g    7
dtype: int64
a    4.0
b    5.0
c    6.0
d    NaN
e    NaN
dtype: float64
a    4
b    5
c    6
d    6
e    6
dtype: int64
a     4
b     5
c     6
d     7
e     7
dtype: int64
obj_4 = obj.reindex(['a','b','c','d','e']).ffill()
print(obj_4)
obj_5 = obj.reindex(['a','b','c','d','e']).bfill()
print(obj_5)
a    4.0
b    5.0
c    6.0
d    6.0
e    6.0
dtype: float64
a    4.0
b    5.0
c    6.0
d    NaN
e    NaN
dtype: float64

算数运算和数据对齐
就是可以对不同的对象进行算数运算,存在不同索引时,则结果的索引就是该索引的并集

obj_6 = Series([1,2,3],index=['a','b','c'])
print(obj_6)
obj_7 = Series([1,2,3,4],index=['a','b','c','d'])
print(obj_7)
a    1
b    2
c    3
dtype: int64
obj_6 + obj_7
a    2.0
b    4.0
c    6.0
d    NaN
dtype: float64
import numpy as np 
df1 = DataFrame(np.arange(9).reshape((3,3)),columns=list('abc'),index = [1,2,3])
print(df1)
df2 = DataFrame(np.arange(16).reshape((4,4)),columns=list('bcde'),index = [1,2,3,4])
print(df2)
df1 + df2
   a  b  c
1  0  1  2
2  3  4  5
3  6  7  8
    b   c   d   e
1   0   1   2   3
2   4   5   6   7
3   8   9  10  11
4  12  13  14  15
a b c d e
1 NaN 1.0 3.0 NaN NaN
2 NaN 8.0 10.0 NaN NaN
3 NaN 15.0 17.0 NaN NaN
4 NaN NaN NaN NaN NaN
df1.add(df2,fill_value=12)#用12来填充不重叠的值,注意本身为空那么还是为空
a b c d e
1 12.0 1.0 3.0 14.0 15.0
2 15.0 8.0 10.0 18.0 19.0
3 18.0 15.0 17.0 22.0 23.0
4 NaN 24.0 25.0 26.0 27.0

展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 数字20 设计师: CSDN官方博客
应支付0元
点击重新获取
扫码支付

支付成功即可阅读