Using Python for data analysis _pandas_ Foundation _2

Source: Internet
Author: User

Reindex method of basic ability re-indexing series
in [+]: obj = Series ([3,2,5,7,6,9,0,1,4,8],index=['a','b','C','D','e','F','g',    ...: 'h','I','J']) in [+]: obj1 = Obj.reindex (['a','b','C','D','e','F','g','h','I','J','k']) in [17]: obj1out[17]:a3.0b2.0C5.0D7.0e6.0F9.0g0.0h1.0I4.0J8.0k Nandtype:float64

The current value of the new index value is missing, you need to interpolate the value

The forward value fills method= ' Ffill ', and the last index J corresponds to the value to fill

in [+]: obj1 = Obj.reindex (['a','b','C','D','e','F','g','h','I','J','k'],metho ...: D='Ffill') in [20]: obj1out[20]:a3b2C5D7e6F9g 0h1I4J8k8Dtype:int64

Forward value handling method= ' pad ', last index J corresponds to the value to fill

In [all]: Obj1 = Obj.reindex (['a','b','C','D','e','F','g','h','I','J','k'],metho ...: D='Pad') in [24]: obj1out[24]:a3b2C5D7e6F9g 0h1I4J8k8Dtype:int64

The back value is populated with method= ' Bfill ', the index corresponding to the last index J is populated, and the next position of J is a blank line of Nan.

in [+]: Obj2 = Obj.reindex ([' A ', ' B ', ' C ', ' d ', ' e ', ' f ', ' G ', ' k ', ' h ', ' I ', ' J '],metho
...: d= ' Bfill ')

in [+]: 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 trailing value is carried method= ' backfill ', the index corresponding to the last index J is populated, and the next position of J is a blank line of Nan.

in [+]: Obj2 = Obj.reindex (['a','b','C','D','e','F','g','k','h','I','J'],metho ...: D='Backfill') in [65]: obj2out[65]:a3.0b2.0C5.0D7.0e6.0F9.0g0.0k Nanh1.0I4.0J8.0Dtype:float64
The Reindex method of Dataframe

Modify (Row) an index, a column, or two changes.

When a sequence is introduced, the row is re-indexed, as follows:

in [+]: data = {'class':['language','Mathematics','English'],'score': [120,130,140]}in [[]: frame =DataFrame (data) in [88]: frameout[88]:  classScore0 Language1201 Mathematics 1302 English 140In [: frame2 = Frame.reindex ([0,1,2,3]) in [90]: frame2out[90]:  classScore0 Language120.01 Mathematics 130.02 English 140.03 nan nan

Rows and columns are modified

In [94]: Frame3 = frame.reindex (index=[11,22,33],columns = ['a','b') ,'C','d']) in [frame3out[]:  [A]:     a   b   c   D-a nan-nan nan nan-nan-nan-nan nan-nan-nan-nan NaN

The parameters of the Reindex are as follows:

Deletes the item series on the specified axis (index)
in []: obj = Series ([1,2,3,4],index=['a','b','C','D']) in [113]: objout[113]:a1b2C3D4dtype:int64in [[Obj1]: = Obj.drop ('C') in [115]: obj1out[115]:a1b2D4Dtype:int64
DataFrame

Delete a single index row

In [109]: frameout[109]:  class  score0    Chinese     1201    Math    1302    English    in[+]: obj = frame.drop (0) in [111]: objout[111]:   class  score1    math    1302    English    140

To delete a multi-index row

In [119]: frameout[119]:  class  score0    Chinese     1201    Math    1302    English    in[]: Frame.drop ([up]) out[  ]:class   score0    language     120

Delete multiple index rows (with axis)

in [[]: frameout[]  :class  score0    Chinese     1201    Mathematics    1302    English    in[131]: Frame.drop ([1,2],axis=0) out[131]:    Class  score0    language     120

Delete Column (columns) (with axis)

In [135]: frameout[135]:  class  score0    Chinese     1201    Math    1302    English    in[136]: Frame.drop (['class'],axis=1) out[ 136]:   score0    1201    1302    140

Where, axis=0, represents a row, Axis=1, represents a column

Using Python for data analysis _pandas_ Foundation _2

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.