Use Python for data analysis _ Pandas _ basic _ 2, _ pandas_2
Reindex method of Series reindex
In [15]: obj = Series([3,2,5,7,6,9,0,1,4,8],index=['a','b','c','d','e','f','g', ...: 'h','i','j'])In [16]: obj1 = obj.reindex(['a','b','c','d','e','f','g','h','i','j','k'])In [17]: obj1Out[17]:a 3.0b 2.0c 5.0d 7.0e 6.0f 9.0g 0.0h 1.0i 4.0j 8.0k NaNdtype: float64
If the current value of the new index is missing, interpolation is required.
Fill in the forward value with method = 'ffill', and then fill in the value corresponding to index j.
In [19]: obj1 = obj.reindex(['a','b','c','d','e','f','g','h','i','j','k'],metho ...: d='ffill')In [20]: obj1Out[20]:a 3b 2c 5d 7e 6f 9g 0h 1i 4j 8k 8dtype: int64
Forward value handling method = 'pad ', and finally index the value corresponding to j to fill
In [23]: obj1 = obj.reindex(['a','b','c','d','e','f','g','h','i','j','k'],metho ...: d='pad')In [24]: obj1Out[24]:a 3b 2c 5d 7e 6f 9g 0h 1i 4j 8k 8dtype: int64
The backward value is filled with method = 'bfill', and the value corresponding to the index after index j is filled with null rows whose position is NaN.
In [62]: obj2 = obj. reindex (['A', 'B', 'C', 'D', 'E', 'E', 'F', 'G', 'k', 'h ', 'I', 'J'], metho
...: D = 'bfill ')
In [63]: obj2
Out [63]:
A 3.0
B 2.0
C 5.0
D 7.0
E 6.0
F 9.0
G 0.0
K NaN
H 1.0
I 4.0
J 8.0
Dtype: float64
The method = 'backfill' is carried to the backward value, and the corresponding value of the index after j is filled. The latter position of j is the empty row of NaN.
In [64]: obj2 = obj.reindex(['a','b','c','d','e','f','g','k','h','i','j'],metho ...: d='backfill')In [65]: obj2Out[65]:a 3.0b 2.0c 5.0d 7.0e 6.0f 9.0g 0.0k NaNh 1.0i 4.0j 8.0dtype: float64
DataFrame reindex Method
Modify (ROW) indexes, columns, or both.
When a sequence is introduced, the row is re-indexed as follows:
In [86]: data = {'class': ['Chinese', 'mat', 'inc'], 'score ': [120,130,140]} In [87]: frame = DataFrame (data) In [88]: frameOut [88]: class score0 language 1201 mathematics 1302 English 140In [89]: frame2 = frame. reindex ([120.01, 130.02]) In [90]: frame2Out [90]: class score0 language 140.03 mathematics English NaN
Modify rows and columns
In [94]: frame3 = frame.reindex(index=[11,22,33],columns = ['a','b','c','d'])In [95]: frame3Out[95]: a b c d11 NaN NaN NaN NaN22 NaN NaN NaN NaN33 NaN NaN NaN NaN
The reindex parameters are as follows:
Delete the Series item on the specified axis (INDEX)
In [112]: obj = Series([1,2,3,4],index=['a','b','c','d'])In [113]: objOut[113]:a 1b 2c 3d 4dtype: int64In [114]: obj1 = obj.drop('c')In [115]: obj1Out[115]:a 1b 2d 4dtype: int64
DataFrame
Delete a single index row
In [109]: frameOut [109]: class score0 language 1201 mathematics 1302 English 140In [110]: obj = frame. drop (0) In [111]: objOut [111]: class score1 mathematics 1302 English 140
Delete multiple index rows
In [119]: frameOut [119]: class score0 Chinese 1201 mathematics 1302 English 140In [120]: frame. drop ([120]) Out [120]: class score0 Chinese
Delete multiple index rows (with axis)
In [130]: frameOut [130]: class score0 language 1201 mathematics 1302 English 140In [131]: frame. drop ([131], axis = 0) Out [120]: class score0 Language
Delete columns (with axis)
In [135]: frameOut [135]: class score0 language 1201 mathematics 1302 English 140In [136]: frame. drop (['class'], axis = 1) Out [136]: score0 1201 1302
Axis = 0 indicates the row, axis = 1 indicates the column