5. Pandas系列 - 重建索引_互联网广告



  • 示例
  • 重建索引与其他对象对齐
  • 填充时重新加注
  • 重建索引时的填充限制
  • 重命名


重新索引会更改DataFrame的行标签和列标签。重新索引意味着符合数据以匹配特定轴上的一组给定的标签。可以通过索引来实现多个操作:


  • 重新排序现有数据以匹配一组新的标签
  • 在没有标签数据的标签位置插入缺失值(NA)标记

示例

import pandas as pd
import numpy as np

N=20

df = pd.DataFrame({
'A': pd.date_range(start='2016-01-01',periods=N,freq='D'),
'x': np.linspace(0,stop=N-1,num=N),
'y': np.random.rand(N),
'C': np.random.choice(['Low','Medium','High'],N).tolist(),
'D': np.random.normal(100, 10, size=(N)).tolist()
})

#reindex the DataFrame
df_reindexed = df.reindex(index=[0,2,5], columns=['A', 'C', 'B'])

print (df_reindexed)

结果:

>>> df
A C D x y
0 2016-01-01 High 114.381446 0.0 0.308304
1 2016-01-02 High 113.903337 1.0 0.845635
2 2016-01-03 Low 88.598431 2.0 0.022341
3 2016-01-04 Medium 110.205763 3.0 0.137682
4 2016-01-05 High 92.871260 4.0 0.233312
5 2016-01-06 High 80.547223 5.0 0.316480
6 2016-01-07 Medium 102.559371 6.0 0.290867
7 2016-01-08 Medium 106.990847 7.0 0.078865
8 2016-01-09 Low 91.785478 8.0 0.867655
9 2016-01-10 High 89.334388 9.0 0.733685
10 2016-01-11 High 102.485193 10.0 0.682129
11 2016-01-12 High 89.719618 11.0 0.149376
12 2016-01-13 High 89.175230 12.0 0.522455
13 2016-01-14 High 80.997986 13.0 0.436974
14 2016-01-15 Medium 107.099049 14.0 0.154620
15 2016-01-16 High 85.012230 15.0 0.217840
16 2016-01-17 Low 118.415446 16.0 0.034683
17 2016-01-18 Medium 101.326426 17.0 0.797601
18 2016-01-19 Low 112.628830 18.0 0.067099
19 2016-01-20 Medium 101.985635 19.0 0.929806
>>> df.reindex(index=[0,2,5],columns=['A', 'C', 'B'])
A C B
0 2016-01-01 High NaN
2 2016-01-03 Low NaN
5 2016-01-06 High NaN



重建索引与其他对象对齐

有时可能希望采取一个对象和重新索引,其 轴 被标记为与另一个对象相同

import pandas as pd
import numpy as np

df1 = pd.DataFrame(np.random.randn(10,3),columns=['col1','col2','col3'])
df2 = pd.DataFrame(np.random.randn(7,3),columns=['col1','col2','col3'])

df1 = df1.reindex_like(df2)
print df1

res:

       col1      col2      col3
0 -0.937012 0.281154 -0.206154
1 -0.387672 -1.161284 -1.053927
2 -0.078375 0.286627 3.185455
3 -0.952543 1.100563 0.873561
4 1.543179 -0.590498 0.569140
5 -0.887682 -0.390340 0.793262
6 0.200928 0.536087 -0.884333

注意 : 在这里,df1数据帧(DataFrame)被更改并重新编号,如df2。列名称应该匹配,否则将为整个列标签添加NAN。




填充时重新加注

reindex()采用可选参数方法,它是一个填充方法 其值如下:


  • pad/ffill - 向前填充值
  • bfill/backfill - 向后填充值
  • nearest - 从最近的索引值填充

import pandas as pd
import numpy as np

df1 = pd.DataFrame(np.random.randn(6,3),columns=['col1','col2','col3'])
df2 = pd.DataFrame(np.random.randn(2,3),columns=['col1','col2','col3'])

# Padding NAN's
print df2.reindex_like(df1)

# Now Fill the NAN's with preceding Values
print ("Data Frame with Forward Fill:")
print df2.reindex_like(df1,method='ffill')

res:

       col1      col2      col3
0 -0.109234 -0.757402 -0.167979
1 -1.478847 0.030590 -0.062580
2 NaN NaN NaN
3 NaN NaN NaN
4 NaN NaN NaN
5 NaN NaN NaN

Data Frame with Forward Fill:
col1 col2 col3
0 -0.109234 -0.757402 -0.167979
1 -1.478847 0.030590 -0.062580
2 -1.478847 0.030590 -0.062580
3 -1.478847 0.030590 -0.062580
4 -1.478847 0.030590 -0.062580
5 -1.478847 0.030590 -0.062580




重建索引时的填充限制

制参数在重建索引时提供对填充的额外控制。限制指定连续匹配的最大计数

import pandas as pd
import numpy as np

df1 = pd.DataFrame(np.random.randn(6,3),columns=['col1','col2','col3'])
df2 = pd.DataFrame(np.random.randn(2,3),columns=['col1','col2','col3'])

# Padding NAN's
print df2.reindex_like(df1)

# Now Fill the NAN's with preceding Values
print ("Data Frame with Forward Fill limiting to 1:")
print df2.reindex_like(df1,method='ffill',limit=1)

res:

       col1      col2      col3
0 0.112024 -1.431111 -0.828381
1 -0.182373 -0.025696 -1.673949
2 NaN NaN NaN
3 NaN NaN NaN
4 NaN NaN NaN
5 NaN NaN NaN

Data Frame with Forward Fill limiting to 1:
col1 col2 col3
0 0.112024 -1.431111 -0.828381
1 -0.182373 -0.025696 -1.673949
2 -0.182373 -0.025696 -1.673949
3 NaN NaN NaN
4 NaN NaN NaN
5 NaN NaN NaN




重命名

rename()方法允许基于一些映射(字典或者系列)或任意函数来重新标记一个轴 参数有 column和index

import pandas as pd
import numpy as np

df1 = pd.DataFrame(np.random.randn(6,3),columns=['col1','col2','col3'])
print df1

print ("After renaming the rows and columns:")
print df1.rename(columns={'col1' : 'c1', 'col2' : 'c2'},
index = {0 : 'apple', 1 : 'banana', 2 : 'durian'}, inplace = True)





作者:Johngo

配图:Pexels




 

5. Pandas系列 - 重建索引_互联网广告_02


互联网广告收入占到互联网收入的80%以上计算广告,一起研究流量变现,欢迎大家的加入