Realization of ReIndex Re indexing Based on Pandas

  • 2021-07-01 07:48:25
  • OfStack

Agreement:


import pandas as pd
import numpy as np

ReIndex reindexing

reindex () is an important method of pandas objects, which is used to create a new object for a new index.

1. Re-index the Series object


se1=pd.Series([1,7,3,9],index=['d','c','a','f'])
se1

Code result:

d 1
c 7
a 3
f 9
dtype: int64

Calling reindex will reorder, and missing values will be filled with NaN.


se2=se1.reindex(['a','b','c','d','e','f'])
se2

Code result:

a 3.0
b NaN
c 7.0
d 1.0
e NaN
f 9.0
dtype: float64

Pass in method= "" When re-indexing, select interpolation processing mode:

method = 'ffill' or 'pad Forward Fill

method = 'bfill' or 'backfill backfill


se3=pd.Series(['blue','red','black'],index=[0,2,4])
se4=se3.reindex(range(6),method='ffill')
se4

Code result:

0 blue
1 blue
2 red
3 red
4 black
5 black
dtype: object

2. Re-index the DataFrame object

For DataFrame objects, reindex can modify row indexes and column indexes.


df1=pd.DataFrame(np.arange(9).reshape(3,3),index=['a','c','d'],columns=['one','two','four'])
df1

Code result:

one two four
a 0 1 2
c 3 4 5
d 6 7 8

Row index reordering by default

Sequence index cannot be rearranged if only 1 sequence is passed in


df1.reindex(['a','b','c','d'])

Code result:

one two four
a 0.0 1.0 2.0
b NaN NaN NaN
c 3.0 4.0 5.0
d 6.0 7.0 8.0


df1.reindex(index=['a','b','c','d'],columns=['one','two','three','four'])

Code result:

one two three four
a 0.0 1.0 NaN 2.0
b NaN NaN NaN NaN
c 3.0 4.0 NaN 5.0
d 6.0 7.0 NaN 8.0

Incoming fill_value=n Replace the missing value with n:


df1.reindex(index=['a','b','c','d'],columns=['one','two','three','four'],fill_value=100)

Code result:

one two three four
a 0 1 100 2
b 100 100 100 100
c 3 4 100 5
d 6 7 100 8


Related articles: