博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
pandas的基本用法(二)——选择数据
阅读量:3979 次
发布时间:2019-05-24

本文共 2052 字,大约阅读时间需要 6 分钟。

文章作者:Tyan

博客:  |   | 

本文主要是关于pandas的一些基本用法。

#!/usr/bin/env python# _*_ coding: utf-8 _*_import pandas as pdimport numpy as np# Test 1# 定义数据dates = pd.date_range('20170101', periods = 6)print datesdf = pd.DataFrame(np.arange(24).reshape((6, 4)), index = dates, columns = ['A', 'B', 'C', 'D'])print df# Test 1 resultDatetimeIndex(['2017-01-01', '2017-01-02', '2017-01-03', '2017-01-04',               '2017-01-05', '2017-01-06'],              dtype='datetime64[ns]', freq='D')             A   B   C   D2017-01-01   0   1   2   32017-01-02   4   5   6   72017-01-03   8   9  10  112017-01-04  12  13  14  152017-01-05  16  17  18  192017-01-06  20  21  22  23# Test 2# 选择第一列数据print df['A']print df.A# 选择前三行数据print df[0:3]print df['20170101':'20170103']# 根据标签选择print df.loc['20170101']# 选择所有行, 特定列print df.loc[:, ['A', 'B']]# 选择特定行, 特定列print df.loc['20170102', ['A', 'B']]# Test 2 result2017-01-01     02017-01-02     42017-01-03     82017-01-04    122017-01-05    162017-01-06    20Freq: D, Name: A, dtype: int642017-01-01     02017-01-02     42017-01-03     82017-01-04    122017-01-05    162017-01-06    20Freq: D, Name: A, dtype: int64            A  B   C   D2017-01-01  0  1   2   32017-01-02  4  5   6   72017-01-03  8  9  10  11            A  B   C   D2017-01-01  0  1   2   32017-01-02  4  5   6   72017-01-03  8  9  10  11A    0B    1C    2D    3Name: 2017-01-01 00:00:00, dtype: int64             A   B2017-01-01   0   12017-01-02   4   52017-01-03   8   92017-01-04  12  132017-01-05  16  172017-01-06  20  21A    4B    5Name: 2017-01-02 00:00:00, dtype: int64# Test 3# 根据行列来选择print df.iloc[3:5, 1:3]# 不连续的选择print df.iloc[[1, 3, 5], 2:4]# 混合选择print df.ix[[1, 3, 5], ['A', 'B']]# 对比选择print df[df.A > 4]# Test 3 result             B   C2017-01-04  13  142017-01-05  17  18             C   D2017-01-02   6   72017-01-04  14  152017-01-06  22  23             A   B2017-01-02   4   52017-01-04  12  132017-01-06  20  21             A   B   C   D2017-01-03   8   9  10  112017-01-04  12  13  14  152017-01-05  16  17  18  192017-01-06  20  21  22  23

参考资料

转载地址:http://rcwui.baihongyu.com/

你可能感兴趣的文章
onTouchEvent方法的使用
查看>>
Android详细解释键盘和鼠标事件
查看>>
如何成为强大的程序员?
查看>>
打包时sun.misc.ServiceConfigurationError
查看>>
摘自 管理自己[Managing Oneself]
查看>>
程序员开发大型应用程序的技巧
查看>>
远程团队管理的10条戒律
查看>>
在服务器上排除问题的头五分钟
查看>>
Diagnosing DFC Configuration Problems
查看>>
jboss java.lang.NoClassDefFoundError: Could not initialize class com.documentum.fc.client.DfClient
查看>>
芯片常见封装
查看>>
什么是oc门
查看>>
上拉电阻 下拉电阻的汇总
查看>>
NTC热敏电阻的基本特性
查看>>
数字地和模拟地处理的基本原则
查看>>
集电极开路,漏极开路,推挽,上拉电…
查看>>
长尾式差分放大电路2
查看>>
十种精密整流电路
查看>>
红外线遥控原理
查看>>
放大电路的主要性能指标?
查看>>