经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 程序设计 » Python3 » 查看文章
Python3.5 Pandas模块之DataFrame用法实例分析
来源:jb51  时间:2019/4/24 9:44:49  对本文有异议

本文实例讲述了Python3.5 Pandas模块之DataFrame用法。分享给大家供大家参考,具体如下:

1、DataFrame的创建

(1)通过二维数组方式创建


  1. #!/usr/bin/env python
  2. # -*- coding:utf-8 -*-
  3. # Author:ZhengzhengLiu
  4.  
  5. import numpy as np
  6. import pandas as pd
  7. from pandas import Series,DataFrame
  8.  
  9. #1.DataFrame通过二维数组创建
  10. print("======DataFrame直接通过二维数组创建======")
  11. d1 = DataFrame([["a","b","c","d"],[1,2,3,4]])
  12. print(d1)
  13.  
  14. print("======DataFrame借助array二维数组创建======")
  15. arr = np.array([
  16. ["jack",78],
  17. ["lili",86],
  18. ["amy",97],
  19. ["tom",100]
  20. ])
  21.  
  22. d2 = DataFrame(arr,index=["01","02","03","04"],columns=["姓名","成绩"])
  23. print(d2)
  24. print("========打印行索引========")
  25. print(d2.index)
  26. print("========打印列索引========")
  27. print(d2.columns)
  28. print("========打印值========")
  29. print(d2.values)
  30.  

运行结果:

======DataFrame直接通过二维数组创建======
   0  1  2  3
0  a  b  c  d
1  1  2  3  4
======DataFrame借助array二维数组创建======
      姓名   成绩
01  jack   78
02  lili   86
03   amy   97
04   tom  100
========打印行索引========
Index(['01', '02', '03', '04'], dtype='object')
========打印列索引========
Index(['姓名', '成绩'], dtype='object')
========打印值========
[['jack' '78']
 ['lili' '86']
 ['amy' '97']
 ['tom' '100']]

(2)通过字典方式创建


  1. #2.DataFrame通过字典创建,键作为列索引,键值作为数据值,行索引值自动生成
  2.  
  3. data = {
  4. "apart":['1101',"1102","1103","1104"],
  5. "profit":[2000,4000,5000,3500],
  6. "month":8
  7. }
  8.  
  9. d3 = DataFrame(data)
  10. print(d3)
  11. print("========行索引========")
  12. print(d3.index)
  13. print("========列索引========")
  14. print(d3.columns)
  15. print("========数据值========")
  16. print(d3.values)

运行结果:

apart  month  profit
0  1101      8    2000
1  1102      8    4000
2  1103      8    5000
3  1104      8    3500
========行索引========
RangeIndex(start=0, stop=4, step=1)
========列索引========
Index(['apart', 'month', 'profit'], dtype='object')
========数据值========
[['1101' 8 2000]
 ['1102' 8 4000]
 ['1103' 8 5000]
 ['1104' 8 3500]]

2、DataFrame数据获取




  1. import numpy as np
  2. import pandas as pd
  3. from pandas import Series,DataFrame
  4.  
  5. #3.DataFrame获取数据
  6. data = {
  7. "apart":['1101',"1102","1103","1104"],
  8. "profit":[2000,4000,5000,3500],
  9. "month":8
  10. }
  11.  
  12. d3 = DataFrame(data)
  13. print(d3)
  14.  
  15. print("======获取一列数据======")
  16. print(d3["apart"])
  17. print("======获取一行数据======")
  18. print(d3.ix[1])
  19.  
  20. print("======修改数据值======")
  21. d3["month"] = [7,8,9,10] #修改值
  22. d3["year"] = [2001,2001,2003,2004] #新增列
  23. d3.ix["4"] = np.NaN
  24. print(d3)
  25.  

运行结果:

 apart  month  profit
0  1101      8    2000
1  1102      8    4000
2  1103      8    5000
3  1104      8    3500
======获取一列数据======
0    1101
1    1102
2    1103
3    1104
Name: apart, dtype: object
======获取一行数据======
apart     1102
month        8
profit    4000
Name: 1, dtype: object
======修改数据值======
  apart  month  profit    year
0  1101    7.0  2000.0  2001.0
1  1102    8.0  4000.0  2001.0
2  1103    9.0  5000.0  2003.0
3  1104   10.0  3500.0  2004.0
4   NaN    NaN     NaN     NaN

3、pandas基本功能


(1)pandas数据文件读取



  1. import numpy as np
  2. import pandas as pd
  3. from pandas import Series,DataFrame
  4.  
  5. #pandas基本操作
  6. #1.数据文件读取
  7.  
  8. df = pd.read_csv("data.csv")
  9. print(df)
  10.  

运行结果:

    name  age  source
0  gerry   18    98.5
1    tom   21    78.2
2   lili   24    98.5
3   john   20    89.2

(2)数据过滤获取


  1. import numpy as np
  2. import pandas as pd
  3. from pandas import Series,DataFrame
  4.  
  5. #pandas基本操作
  6. #1.数据文件读取
  7.  
  8. df = pd.read_csv("data.csv")
  9. print(df)
  10.  
  11. #2.数据过滤获取
  12.  
  13. columns = ["姓名","年龄","成绩"]
  14. df.columns = columns #更改列索引
  15. print("=======更改列索引========")
  16. print(df)
  17.  
  18. #获取几列的值
  19. df1 = df[columns[1:]]
  20. print("=======获取几列的值========")
  21. print(df1)
  22. print("=======获取几行的值========")
  23. print(df.ix[1:3])
  24.  
  25. #删除含有NaN值的行
  26. df2 = df1.dropna()
  27. print("=======删除含有NaN值的行=======")
  28. print(df2)
  29.  

运行结果:

 name  age  source
0  gerry   18    98.5
1    tom   21     NaN
2   lili   24    98.5
3   john   20    89.2
=======更改列索引========
      姓名  年龄    成绩
0  gerry  18  98.5
1    tom  21   NaN
2   lili  24  98.5
3   john  20  89.2
=======获取几列的值========
   年龄    成绩
0  18  98.5
1  21   NaN
2  24  98.5
3  20  89.2
=======获取几行的值========
     姓名  年龄    成绩
1   tom  21   NaN
2  lili  24  98.5
3  john  20  89.2
=======删除含有NaN值的行=======
   年龄    成绩
0  18  98.5
2  24  98.5
3  20  89.2

更多关于Python相关内容感兴趣的读者可查看jb51专题:《Python数学运算技巧总结》、《Python数据结构与算法教程》、《Python函数使用技巧总结》、《Python字符串操作技巧汇总》、《Python入门与进阶经典教程》及《Python文件与目录操作技巧汇总

希望本文所述对大家Python程序设计有所帮助。

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号