Pandas:DataFrame数据选择方法(索引)

#首先创建我们的Series对象,然后合并到dataframe对象里面去
import pandas as pd
import numpy as np
area=pd.Series({'ChongQing':188888,'BeiJing':92387928,'Shanghai':8374583746,'Sydney':82734})
population=pd.Series({'ChongQing':1000,'BeiJing':2000,'Shanghai':2900,'Sydney':3000})
data=pd.DataFrame({'area':area,'population':population})#备注:创建字典的结构时一定要遵循字典的数据结构
#也就是创建完字典之后一定要在字典的前后写上花括号,这个是一个很重要的习惯
print(data)

输出结果:

                 area        population
ChongQing      188888        1000
BeiJing      92387928        2000
Shanghai   8374583746        2900
Sydney          82734        3000

输入代码增加我们colums上的对象:

data['area']

输出:

ChongQing        188888
BeiJing        92387928
Shanghai     8374583746
Sydney            82734
Name: area, dtype: int64

输入:

#利用属性的形式来列出一个columns的数据,上面是使用了索引的形式,这种形式并不太常用
data.area

输出:

ChongQing        188888
BeiJing        92387928
Shanghai     8374583746
Sydney            82734
Name: area, dtype: int64

输入:

data.values#其实dataframe是一个十分显然的二维数组,我们可以用这个公式来验证它

输出:

array([[1.88888000e+05, 1.00000000e+03, 1.88888000e+02],
       [9.23879280e+07, 2.00000000e+03, 4.61939640e+04],
       [8.37458375e+09, 2.90000000e+03, 2.88778750e+06],
       [8.27340000e+04, 3.00000000e+03, 2.75780000e+01]])

 

posted @ 2019-06-23 13:01  Geeksongs  阅读(2967)  评论(0编辑  收藏  举报

Coded by Geeksongs on Linux

All rights reserved, no one is allowed to pirate or use the document for other purposes.