主要内容:

  • 创建数据表
  • 查看数据表
  • 数据表索引、选取部分数据
    • 通过标签选取.loc
    • 多重索引选取
    • 位置选取.iloc
    • 布尔索引

Object Creation 新建数据

  • 用list建series序列
In [73]: s = pd.Series([1,3,5,np.nan,6,8])

In [74]: s
Out[74]:
0 1.0
1 3.0
2 5.0
3 NaN
4 6.0
5 8.0
dtype: float64
  • 用numpy array建dataframe
In [75]: dates = pd.date_range('20130101', periods=6)

In [76]: df = pd.DataFrame(np.random.randn(6,4), index=dates, columns=list('ABCD'))

In [77]: df
Out[77]:
A B C D
2013-01-01 -0.411674 0.273549 0.629843 1.881497
2013-01-02 1.240512 0.970725 0.033099 1.553420
2013-01-03 -0.544326 0.545738 -1.325810 0.130738
2013-01-04 1.044803 -0.117151 0.874583 2.278227
2013-01-05 -2.194728 -2.536257 0.478644 0.057728
2013-01-06 -1.092031 1.249952 1.598761 -0.153423 #---pd.date_range?---
In [115]: pd.date_range(start='12/31/2011', end='12/31/2013', freq='A')
Out[115]: DatetimeIndex(['2011-12-31', '2012-12-31', '2013-12-31'], dtype='datetime64[ns]', freq='A-DEC')
  • 用dictionary
In [78]: df2 = pd.DataFrame({ 'A' : 1.,
...: 'B' : pd.Timestamp('20130102'),
...: 'C' : pd.Series(1,index=list(range(4)),dtype='float32'),
...: 'D' : np.array([3] * 4,dtype='int32'),
...: 'E' : pd.Categorical(["test","train","test","train"]),
...: 'F' : 'foo' })
...: df2
...:
Out[78]:
A B C D E F
0 1.0 2013-01-02 1.0 3 test foo
1 1.0 2013-01-02 1.0 3 train foo
2 1.0 2013-01-02 1.0 3 test foo
3 1.0 2013-01-02 1.0 3 train foo In [80]: df2.dtypes
Out[80]:
A float64
B datetime64[ns]
C float32
D int32
E category
F object
dtype: object

在ipython中可以使用<tab>键进行自动补充,它会列出数据对象可以执行的操作。

查看数据

df.head()
df.tail(3)
df.index
df.columns #返回一个这样的东西:pandas.indexes.numeric.Int64Index
df.values #提取出数据框的数值,返回一个array

数据选取

建议 使用pandas的数据选取方法:.at, .iat, .loc, .iloc, .ix. 这些更高效。

df['A']       # 选取某一列,返回一个Series,== df.A,【只能选某一列,不能用":"多选。】

df[0:3]       # 选行
df['20130102':'20130104']
  • 通过标签label选取,.loc

    用.loc[]选取数据时,方括号里对应的是:[行,列](逗号分隔),如果只有一个值,默认是行。可以用“:”。

    In [82]: df
    Out[82]:
    A B C D
    2013-01-01 -0.411674 0.273549 0.629843 1.881497
    2013-01-02 1.240512 0.970725 0.033099 1.553420
    2013-01-03 -0.544326 0.545738 -1.325810 0.130738
    2013-01-04 1.044803 -0.117151 0.874583 2.278227
    2013-01-05 -2.194728 -2.536257 0.478644 0.057728
    2013-01-06 -1.092031 1.249952 1.598761 -0.153423 In [83]: df.loc[dates[0]] # 作为index的日期列叫dates
    Out[83]:
    A -0.411674
    B 0.273549
    C 0.629843
    D 1.881497
    Name: 2013-01-01 00:00:00, dtype: float64 #---对多个维度轴axis进行选取---
    In [84]: df.loc['20130102':'20130104',['A','B']]
    Out[84]:
    A B
    2013-01-02 1.240512 0.970725
    2013-01-03 -0.544326 0.545738
    2013-01-04 1.044803 -0.117151 #---选取某个数值---
    In [85]: df.loc[dates[0],'A']
    Out[85]: -0.41167416696608039 In [86]: df.at[dates[0],'A'] # 更高效的做法
    Out[86]: -0.41167416696608039
  • 多重索引的选取

    index有多个维度

    #这里有一个多重索引
    MultiIndex(levels=[[1, 2, 3], ['count', 'mean', 'std', 'min', '5%', '10%', '15.0%', '20%', '25%',
    '30.0%', '35%', '40%', '45%', '50%', '55.0%', '60.0%', '65%', '70%',
    '75%', '80%', '85.0%', '90%', '95%', 'max']],
    labels=[[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
    2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2],
    [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
    21, 22, 23, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17,
    18, 19, 20, 21, 22, 23, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
    15, 16, 17, 18, 19, 20, 21, 22, 23]],
    names=['label_1', None]) df[columnName] #选某一列,或多列(":",[,,,])
    df.loc[:,columnName] #选某一列,或多列(":",[,,,])
    df.loc[1,columnName] #可以直接用最外层的索引
    df.loc[(1,'std'),columnName] #多层索引要用tuple。选多行用":"连接tuple。
    df.loc[[(1,'std'),(2,"count")],'feature_001']
  • 用位置选取:.iloc

    .lic[],位置索引,方括号里是整数值。同样的用“,”隔开行列。

    In [93]: df.iloc[3]
    Out[93]:
    A 1.044803
    B -0.117151
    C 0.874583
    D 2.278227
    Name: 2013-01-04 00:00:00, dtype: float64 In [94]: df.iloc[3:5,0:2]
    Out[94]:
    A B
    2013-01-04 1.044803 -0.117151
    2013-01-05 -2.194728 -2.536257 In [95]: df.iat[1,1]
    Out[95]: 0.97072539301549565
  • **布尔索引 **Boolean Indexing

    某一列大于0的数据

    In [96]: df[df.A > 0]
    Out[96]:
    A B C D
    2013-01-02 1.240512 0.970725 0.033099 1.553420
    2013-01-04 1.044803 -0.117151 0.874583 2.278227

    整体大于零的数据。小于0的为NaN

    In [97]: df[df > 0]
    Out[97]:
    A B C D
    2013-01-01 NaN 0.273549 0.629843 1.881497
    2013-01-02 1.240512 0.970725 0.033099 1.553420
    2013-01-03 NaN 0.545738 NaN 0.130738
    2013-01-04 1.044803 NaN 0.874583 2.278227
    2013-01-05 NaN NaN 0.478644 0.057728
    2013-01-06 NaN 1.249952 1.598761 NaN

    对字符型数据选取

    #---isin ---
    In [98]: df2 = df.copy()
    ...: df2['E'] = ['one', 'one','two','three','four','three']
    ...: df2
    ...:
    Out[98]:
    A B C D E
    2013-01-01 -0.411674 0.273549 0.629843 1.881497 one
    2013-01-02 1.240512 0.970725 0.033099 1.553420 one
    2013-01-03 -0.544326 0.545738 -1.325810 0.130738 two
    2013-01-04 1.044803 -0.117151 0.874583 2.278227 three
    2013-01-05 -2.194728 -2.536257 0.478644 0.057728 four
    2013-01-06 -1.092031 1.249952 1.598761 -0.153423 three In [99]: df2[df2['E'].isin(['two','four'])]
    Out[99]:
    A B C D E
    2013-01-03 -0.544326 0.545738 -1.325810 0.130738 two
    2013-01-05 -2.194728 -2.536257 0.478644 0.057728 four

    使用布尔面具

    In [107]: mask = df2["A"] >0
    
    In [108]: df3 = df2[mask]
    
    In [109]: df3
    Out[109]:
    A B C D E
    2013-01-02 1.240512 0.970725 0.033099 1.553420 ONE
    2013-01-04 1.044803 -0.117151 0.874583 2.278227 THREE # 查看无重复的值:.unique()
    In [101]: df2.loc[:,"E"].unique()
    Out[101]: array(['one', 'two', 'three', 'four'], dtype=object)

Python数据分析_Pandas01_数据框的创建和选取的更多相关文章

  1. python数据分析笔记——数据加载与整理]

    [ python数据分析笔记——数据加载与整理] https://mp.weixin.qq.com/s?__biz=MjM5MDM3Nzg0NA==&mid=2651588899&id ...

  2. Python中dataframe数据框中选择某一列非空的行

    利用pandas自带的函数notnull可以很容易判断某一列是否为null类型,但是如果这一列中某一格为空字符串"",此时notnull函数会返回True,而一般我们选择非空行并不 ...

  3. Python数据分析--------numpy数据打乱

    一.shuffle函数: import numpy.random def shuffleData(data): np.random.shufflr(data) cols=data.shape[1] X ...

  4. (数据科学学习手札06)Python在数据框操作上的总结(初级篇)

    数据框(Dataframe)作为一种十分标准的数据结构,是数据分析中最常用的数据结构,在Python和R中各有对数据框的不同定义和操作. Python 本文涉及Python数据框,为了更好的视觉效果, ...

  5. Python数据分析之pandas学习

    Python中的pandas模块进行数据分析. 接下来pandas介绍中将学习到如下8块内容:1.数据结构简介:DataFrame和Series2.数据索引index3.利用pandas查询数据4.利 ...

  6. Pandas系列(二)- DataFrame数据框

    一.初识DataFrame dataFrame 是一个带有索引的二维数据结构,每列可以有自己的名字,并且可以有不同的数据类型.你可以把它想象成一个 excel 表格或者数据库中的一张表DataFram ...

  7. python 数据分析--pandas

    接下来pandas介绍中将学习到如下8块内容:1.数据结构简介:DataFrame和Series2.数据索引index3.利用pandas查询数据4.利用pandas的DataFrames进行统计分析 ...

  8. Python数据分析之pandas

    Python中的pandas模块进行数据分析. 接下来pandas介绍中将学习到如下8块内容:1.数据结构简介:DataFrame和Series2.数据索引index3.利用pandas查询数据4.利 ...

  9. Python数据分析与展示[第三周](pandas简介与数据创建)

    第三周的课程pandas 分析数据 http://pandas.pydata.org import pandas as pd 常与numpy matplotlib 一块定义 d=pd.Series(r ...

随机推荐

  1. am335x Qt SocketCAN Demo hacking

    /*********************************************************************************** * am335x Qt Soc ...

  2. 洛谷 1192:台阶问题(递推,DP)

    题目描述 有 N 级的台阶,你一开始在底部,每次可以向上迈最多 K 级台阶(最少 1 级),问到达第 N 级台阶有多少种不同方式. 输入输出格式 输入格式: 两个正整数N,K. 输出格式: 一个正整数 ...

  3. Jquery中.attr与.prop的区别

    ☆ http://www.jb51.net/article/114876.htm http://www.365mini.com/page/jquery-attr-vs-prop.htm https:/ ...

  4. Sencha Touch+PhoneGap打造超级奶爸之喂养记(一) 源码免费提供(转)

    起源 非常高兴我的宝宝健康平安的出生了.对于初次做奶爸的我,喜悦过后,面临着各中担心,担心宝宝各项指标是否正常.最初几天都是在医院待着,从出生那一天开始,护士妹妹隔一段时间就会来问宝宝的喂奶,大小便, ...

  5. Vquery PHP 简单爬虫类

    http://www.thinkphp.cn/topic/36693.html 在使用php进行网页抓取的时候你有没有感觉到用起来比较麻烦呢?目前我还没有发现php有这样针对网页抓取的类,每次用到这个 ...

  6. juc包下的集合类

    import java.util.Iterator;import java.util.concurrent.CopyOnWriteArrayList; /** * 集合在多线程中同步的方式: * 1. ...

  7. 如何在MFC DLL中向C#类发送消息

    如何在MFC DLL中向C#类发送消息 一. 引言 由于Windows Message才是Windows平台的通用数据流通格式,故在跨语言传输数据时,Message是一个不错的选择,本文档将描述如何在 ...

  8. 把存储过程SELECT INTO到临时表

    在开发过程中,很多时候要把结果集存放到临时表中,常用的方法有两种. 一. SELECT INTO1. 使用select into会自动生成临时表,不需要事先创建12 select * into #te ...

  9. MySQL5.5.19安装

    本文详细介绍了Windows下安装MySQL5.5.19的全过程,希望对初学者有帮助.  http://dl.pconline.com.cn/html_2/1/79/id=465&pn=0.h ...

  10. android 更新listview 其中一行的数据显示

    private void updateView(int index){ View v = yourListView.getChildAt(index - yourListView.getFirstVi ...