Pandas——DataFrame函数使用
2021/5/7 18:31:15
本文主要是介绍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: objectName 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
这篇关于Pandas——DataFrame函数使用的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-11-25JAVA语音识别项目项目实战入门教程
- 2024-11-25JAVA云原生项目实战入门教程
- 2024-11-25Java语音识别项目入门:新手必读指南
- 2024-11-25Java语音识别项目入门:轻松开始你的第一个语音识别项目
- 2024-11-25Java语音识别项目入门详解
- 2024-11-25Java语音识别项目教程:从零开始的详细指南
- 2024-11-25JAVA语音识别项目教程:初学者指南
- 2024-11-25Java语音识别项目教程:初学者指南
- 2024-11-25JAVA云原生入门:新手指南与基础教程
- 2024-11-25Java云原生入门:从零开始的全面指南