【python DataFrame】Pandas里面的屠龙宝刀DataFrame

# encoding: utf-8
from __future__ import division
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
import numpy as np
import pandas as pd
# 1.创建DataFrame的几种方式
#######1.1纯字典创建
students={'names':['Leo','Jack','James'],'scores':[100,90,80]}
df=pd.DataFrame(students)
print df

######1.2字典加列表
scores={'Scores':[100,90,80]}
names=['Leo','Jack','James']
df=pd.DataFrame(scores,index=names)
print df


#先把字典扩展一下,加上Ages键值对
ages={'Ages':[20,23,25]}
scores.update(ages)
df=pd.DataFrame(scores,index=names)
print df


# 1.3用NumPy数组的创建
df = pd.DataFrame(np.arange(9).reshape(3,3))
print df




#行和列取名字
# 行和列都是pandas取默认的数值,如果我们自定义行和列的名字,可以按照下面的形式,加上index和columns关键字
df= pd.DataFrame(np.arange(9).reshape(3, 3),index=['row1','row2','row3'],columns=['col1','col2','col3'])

print df

print df.describe()
#看看它的describe函数都显示啥
# 跟Series的很类似,只是按照每一列进行统计

###############2.索引选取,切片

students=pd.DataFrame({'Name':['Leo','Jack','Lili'],
                        'Scores':[100,90,80],
                        'Sex':['M','M','W']})

print students


# 1).获取某一列的数据
print students['Name']#也可以students.Name

# 有同学说,这不就是前面讲的Series吗,对啊,不行我们打一下type看看
print type(students['Name'])



# 2).获取某一行的数据,用ix(index of label)
print students.ix[0]

# 看pandas多贴心,把列名也显示出来了


# 3).切片获取多行
print students[0:2]

# 4),切片获取多列
print students[['Name','Sex']]

# 或者只想取前两列,前两行
print students.ix[0:1,[0,1]]

######################## 3.修改和删除


students['Scores']=100
print students

# 2).增加一列
students['hobby']=['music','movie','singing']
print students


# 3).删除某列,比如删掉Sex列
# del students['Sex']
print students

############################### 4.过滤数据


#比如:过滤出学生的成绩大于等于90分的
print students[students.Scores>=90]

#比如:过滤出列里是女生的数据
print students[students.Sex=='W']

citys = ['ShangHai', 'BeiJing', 'NanJing', 'HangZhou', 'WuHan',
         'JiNan', 'FuZhou', 'GuangZhou', 'ChongQing', 'HaiKou']

House_Prices = [44750, 48847, 22428, 18900, 12332,
                11423, 16833, 20874, 6870, 6903]

Up_Rate = [31.57, 32.09, 28.95, 2.86, 24,
           16.59, 18.78, 4.77, -2.4, -1.26]

Avg_Salary = [8962, 9240, 6680, 7330, 6331,
              6067, 6522, 7409, 6584, 5827]


pd=pd.DataFrame({'Avg Housing Price':House_Prices,'Up Rate':Up_Rate,'Avg Salary':Avg_Salary},index=citys)
print pd

# 1).数据排个序,取前三名
# 上面的数据并没有排序,我们分别按照房价的高低,涨幅的高低和薪资的高度排个序

#最高房价前3名
print pd.sort_values(by='Avg Housing Price',ascending=False)[:3]

#涨幅最大前3名
print pd.sort_values(by='Up Rate',ascending=False).head()

#平均工资最高前3名
print pd.sort_values(by='Up Rate',ascending=False)[:3]

#有没有哪个城市的房价是跌的
print pd[pd['Up Rate']<0]


#十大城市平均房价,平均工资
print pd.mean()

运行结果:

"D:\Program Files\Python27\python.exe" D:/PycharmProjects/learn2017/wordcloud.py
   names  scores
0    Leo     100
1   Jack      90
2  James      80
       Scores
Leo       100
Jack       90
James      80
       Ages  Scores
Leo      20     100
Jack     23      90
James    25      80
   0  1  2
0  0  1  2
1  3  4  5
2  6  7  8
      col1  col2  col3
row1     0     1     2
row2     3     4     5
row3     6     7     8
       col1  col2  col3
count   3.0   3.0   3.0
mean    3.0   4.0   5.0
std     3.0   3.0   3.0
min     0.0   1.0   2.0
25%     1.5   2.5   3.5
50%     3.0   4.0   5.0
75%     4.5   5.5   6.5
max     6.0   7.0   8.0
   Name  Scores Sex
0   Leo     100   M
1  Jack      90   M
2  Lili      80   W
0     Leo
1    Jack
2    Lili
Name: Name, dtype: object
<class 'pandas.core.series.Series'>
Name      Leo
Scores    100
Sex         M
Name: 0, dtype: object
   Name  Scores Sex
0   Leo     100   M
1  Jack      90   M
   Name Sex
0   Leo   M
1  Jack   M
2  Lili   W
   Name  Scores
0   Leo     100
1  Jack      90
   Name  Scores Sex
0   Leo     100   M
1  Jack     100   M
2  Lili     100   W
   Name  Scores Sex    hobby
0   Leo     100   M    music
1  Jack     100   M    movie
2  Lili     100   W  singing
   Name  Scores Sex    hobby
0   Leo     100   M    music
1  Jack     100   M    movie
2  Lili     100   W  singing
   Name  Scores Sex    hobby
0   Leo     100   M    music
1  Jack     100   M    movie
2  Lili     100   W  singing
   Name  Scores Sex    hobby
2  Lili     100   W  singing
           Avg Housing Price  Avg Salary  Up Rate
ShangHai               44750        8962    31.57
BeiJing                48847        9240    32.09
NanJing                22428        6680    28.95
HangZhou               18900        7330     2.86
WuHan                  12332        6331    24.00
JiNan                  11423        6067    16.59
FuZhou                 16833        6522    18.78
GuangZhou              20874        7409     4.77
ChongQing               6870        6584    -2.40
HaiKou                  6903        5827    -1.26
          Avg Housing Price  Avg Salary  Up Rate
BeiJing               48847        9240    32.09
ShangHai              44750        8962    31.57
NanJing               22428        6680    28.95
          Avg Housing Price  Avg Salary  Up Rate
BeiJing               48847        9240    32.09
ShangHai              44750        8962    31.57
NanJing               22428        6680    28.95
WuHan                 12332        6331    24.00
FuZhou                16833        6522    18.78
          Avg Housing Price  Avg Salary  Up Rate
BeiJing               48847        9240    32.09
ShangHai              44750        8962    31.57
NanJing               22428        6680    28.95
           Avg Housing Price  Avg Salary  Up Rate
ChongQing               6870        6584    -2.40
HaiKou                  6903        5827    -1.26
Avg Housing Price    21016.000
Avg Salary            7095.200
Up Rate                 15.595
dtype: float64

Process finished with exit code 0
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

东华果汁哥

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值