Path to mathematics-python Data Processing (2)-python Data Processing

Source: Internet
Author: User

Path to mathematics-python Data Processing (2)-python Data Processing

Insert column

#-*-Coding: UTF-8 -*-

"""

Created on Mon Mar 09 11:21:02 2015

@ Author: myhaspl@myhaspl.com

"""

Print u "python data analysis \ n"

Import pandas as pd

Import numpy as np

# Constructing product sales data

Mydf = pd. dataFrame ({u'item region Code': [,], u'item a': np. random. randint (, size = 6), u'item B ': np. random. randint (, size = 6), u'item C': np. random. randint (0,1000, size = 6 )})

Allsales = mydf. values [:, 1] + mydf. values [:, 2] + mydf. values [:, 3]

# Insert a column

Mydf. insert (0, u 'total sales', pd. Series (allsales ))

# Sort by two column names first, by followed by the column name, indicating by column name

Mynewdf = mydf. sort_index (axis = 0, by = [u 'region Code', u 'total sales'], ascending = [True, False])

Print mynewdf

 

Python Data Analysis

 

Total sales product A product B Product C product region code

0 1436 805 858 577 1

1 1370 422 606 763 1

3 397 445 41 354 2

5 851 737 629 219 3

2 815 682 133 679 3

4 749 999 521 224 4

>>>

 

The following is an example.

#-*-Coding: UTF-8 -*-

"""

Created on Mon Mar 09 11:21:02 2015

@ Author: myhaspl@myhaspl.com

"""

All content of this blog is original, if reproduced please indicate the source http://blog.csdn.net/myhaspl/

Print u "python data analysis \ n"

Import pandas as pd

Import numpy as np

# Constructing product sales data

Mydf = pd. dataFrame ({u'item region Code': [,], u'item a': np. random. randint (, size = 6), u'item B ': np. random. randint (, size = 6), u'item C': np. random. randint (0,1000, size = 6 )})

Allsales = mydf. values [:, 1] + mydf. values [:, 2] + mydf. values [:, 3]

# Insert a column

Mydf. insert (0, u 'total sales', pd. Series (allsales ))

Print mydf

# Deleting Columns

Mynewdf = mydf. drop ([u 'total sales'], axis = 1)

Print mynewdf

# Moving Columns

Myb = mynewdf. pop (u'item B ')

Mynewdf. insert (2, u 'item B ', myb)

Print mynewdf

 

The result is as follows:

Python Data Analysis

 

Total sales product A product B Product C product region code

0 964 80 940 23 1

1 1188 373 450 737 1

2 1137 907 642 492 3

3 1001 646 952 47 2

4 899 526 19 876 4

5 1225 342 430 792 3

Product A product B Product C product region code

0 80 940 23 1

1 373 450 737 1

2 907 642 492 3

3 646 952 47 2

4 526 19 876 4

5 342 430 792 3

Product A product C product B Product region code

0 80 23 940 1

1 373 737 450 1

2 907 492 642 3

3 646 47 952 2

4 526 876 19 4

5 342 792 430 3

>>>

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.