The road of Mathematics-python Data Processing (2)

Source: Internet
Author: User

Insert Column

#-*-Coding:utf-8-*-

"""

Created on Mon Mar 09 11:21:02 2015

@author: [Email protected]

"""

Print U "python data analysis \ n"

Import Pandas as PD

Import NumPy as NP

#构造商品销量数据

MYDF = PD. DataFrame ({u ' product area code ': [1,1,3,2,4,3],u ' Product A ': Np.random.randint (0,1000,size=6), U ' product B ': Np.random.randint (0,1000, size=6), U ' product C ': Np.random.randint (0,1000,size=6)})

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

#插入一列

Mydf.insert (0,u ' total sales ', PD. Series (Allsales))

#按2个列名先排序, by followed by a column name, indicating by column name

Mynewdf=mydf.sort_index (axis=0, by=[u ' product area code ', U ' total Sales '],ascending=[true,false])

Print MYNEWDF

Python Data analysis

Total sales Product A merchandise B merchandise C product area 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

>>>

Here's an example.

#-*-Coding:utf-8-*-

"""

Created on Mon Mar 09 11:21:02 2015

@author: [Email protected]

"""

This blog all content is original, if reproduced please indicate source http://blog.csdn.net/myhaspl/

Print U "python data analysis \ n"

Import Pandas as PD

Import NumPy as NP

#构造商品销量数据

MYDF = PD. DataFrame ({u ' product area code ': [1,1,3,2,4,3],u ' Product A ': Np.random.randint (0,1000,size=6), U ' product B ': Np.random.randint (0,1000, size=6), U ' product C ': Np.random.randint (0,1000,size=6)})

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

#插入一列

Mydf.insert (0,u ' total sales ', PD. Series (Allsales))

Print MYDF

#删除列

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

Print MYNEWDF

#移动列

Myb=mynewdf.pop (U ' product B ')

Mynewdf.insert (2,u ' merchandise B ', MYB)

Print MYNEWDF

The results are as follows:

Python Data analysis

Total sales Product A merchandise B merchandise C product area 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

Item A merchandise b commodity C product Area 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

Commodity a commodity C commodity B commodity area 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

>>>

The road of Mathematics-python Data Processing (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.