导入导出数据

在导入,导出DataFrame数据时,会用到各种格式,分为 to_csv ;to_excel;to_hdf;to_sql;to_json;to_msgpack ;to_html;to_gbq ;to_stata;to_clipboard;to_pickle

可参照IO Tools分类。

输出指定colums是,会用到arg colums,例如

to_csv(filename,columns=["col1","col2"],......)
# 此处注意的是要使用双引号,单引号不起效果,不知道为什么,另外
# index,header设置为False会不写入行号(索引好)和列标
#也可如下方式使用list函数
to_csv(filename,columns = list('col1','col2'),......)

如果想要保存为ascii文本则可以使用to_csv,可以对是否保存索引(行号)等参数进设置。

调换colums顺序

若原始数据是这样的:

In [6]: df
Out[6]:
0 1 2 3 4 mean
0 0.445598 0.173835 0.343415 0.682252 0.582616 0.445543
1 0.881592 0.696942 0.702232 0.696724 0.373551 0.670208
2 0.662527 0.955193 0.131016 0.609548 0.804694 0.632596
3 0.260919 0.783467 0.593433 0.033426 0.512019 0.436653
4 0.131842 0.799367 0.182828 0.683330 0.019485 0.363371
5 0.498784 0.873495 0.383811 0.699289 0.480447 0.587165
6 0.388771 0.395757 0.745237 0.628406 0.784473 0.588529
7 0.147986 0.459451 0.310961 0.706435 0.100914 0.345149
8 0.394947 0.863494 0.585030 0.565944 0.356561 0.553195
9 0.689260 0.865243 0.136481 0.386582 0.730399 0.561593 In [7]: cols = df.columns.tolist() In [8]: cols
Out[8]: [0L, 1L, 2L, 3L, 4L, 'mean']

通过调换columns更改顺序

In [12]: cols = cols[-1:] + cols[:-1]
In [13]: cols
Out[13]: ['mean', 0L, 1L, 2L, 3L, 4L]

进而可以达到如下效果

In [16]: df = df[cols]  #    OR    df = df.ix[:, cols]

In [17]: df
Out[17]:
mean 0 1 2 3 4
0 0.445543 0.445598 0.173835 0.343415 0.682252 0.582616
1 0.670208 0.881592 0.696942 0.702232 0.696724 0.373551
2 0.632596 0.662527 0.955193 0.131016 0.609548 0.804694
3 0.436653 0.260919 0.783467 0.593433 0.033426 0.512019
4 0.363371 0.131842 0.799367 0.182828 0.683330 0.019485
5 0.587165 0.498784 0.873495 0.383811 0.699289 0.480447
6 0.588529 0.388771 0.395757 0.745237 0.628406 0.784473
7 0.345149 0.147986 0.459451 0.310961 0.706435 0.100914
8 0.553195 0.394947 0.863494 0.585030 0.565944 0.356561
9 0.561593 0.689260 0.865243 0.136481 0.386582 0.730399

参考来源

pandas DataFrame 中指定位置数据的修改:

df['one']['second'] = value
# 由于DataFrame在索引数据是得到的是副本copy所以,此时原数据df并没有修改,并会抛出警告Warning: SettingWithCopy df.loc['one','second'] = value
#如上会修改原数据df
#或是:
dfmi.loc[:,('one','second')] = value

具体参考SettingWithCopy

pandas DataFrame & Series 遍历数据(loop iterate on data)

DataFrame

 dates = pd.date_range("",periods=3)
df = pd.DataFrame(np.random.randn(3,4),index = dates,columns=['A','B','C','D'])
df
dates = pd.date_range("",periods=3)
df = pd.DataFrame(np.random.randn(3,4),index = dates,columns=['A','B','C','D'])
df
Out[36]:
A B C D
2015-01-01 -0.888495 -0.983042 0.162524 -0.768370
2015-01-02 0.954982 0.777860 -0.635805 -0.271617
2015-01-03 1.778827 1.052819 0.090116 -1.822029
  1. DataFrame.iteritems()    :Iterator over (column name, Series) pairs.

     for colName,colSeries in df.iteritems():
    print colName
    print colSeries
     A
    2015-01-01 -0.888495
    2015-01-02 0.954982
    2015-01-03 1.778827
    Freq: D, Name: A, dtype: float64
    B
    2015-01-01 -0.983042
    2015-01-02 0.777860
    2015-01-03 1.052819
    Freq: D, Name: B, dtype: float64
    C
    2015-01-01 0.162524
    2015-01-02 -0.635805
    2015-01-03 0.090116
    Freq: D, Name: C, dtype: float64
    D
    2015-01-01 -0.768370
    2015-01-02 -0.271617
    2015-01-03 -1.822029
    Freq: D, Name: D, dtype: float64
  2. DataFrame.iterrows()    :Iterate over the rows of a DataFrame as (index, Series) pairs. 数据一致是对列来说的,所以此方法迭代时数据类型会改变,如果想使用原始数据类型,最好使用itertuples,且速度快于Itetuples.
     for index,rowSeries in df.iterrows():
    print index
    print rowSeries
     2015-01-01 00:00:00
    A -0.888495
    B -0.983042
    C 0.162524
    D -0.768370
    Name: 2015-01-01 00:00:00, dtype: float64
    2015-01-02 00:00:00
    A 0.954982
    B 0.777860
    C -0.635805
    D -0.271617
    Name: 2015-01-02 00:00:00, dtype: float64
    2015-01-03 00:00:00
    A 1.778827
    B 1.052819
    C 0.090116
    D -1.822029
    Name: 2015-01-03 00:00:00, dtype: float64

     

  3. DataFrame.itertuples(index=True)    :Iterate over the rows of DataFrame as tuples, with index value as first element of the tuple.
     for rowTuple in df.itertuples():
    print rowTuple[0]
    print rowTuple[1:]
     2015-01-01 00:00:00
    (-0.88849501182393553, -0.98304167749573845, 0.1625244406175089, -0.76836987403165646)
    2015-01-02 00:00:00
    (0.95498214900986345, 0.77786021238601544, -0.635805031818656, -0.27161684716624435)
    2015-01-03 00:00:00
    (1.7788269763069902, 1.0528194112440166, 0.09011643978723563, -1.82202928954011)

Series

  1. Series.iteritems()                           :Lazily iterate over (index, value) tuples

     In [51]:
    
     s = pd.Series(['a','b','c','d','e'])
    s
    s = pd.Series(['a','b','c','d','e'])
    s
    Out[51]:
    0 a
    1 b
    2 c
    3 d
    4 e
    dtype: object
     for index,value in s.iteritems():
    print index,value
    0 a
    1 b
    2 c
    3 d
    4 e

pandas的札记的更多相关文章

  1. pandas基础-Python3

    未完 for examples: example 1: # Code based on Python 3.x # _*_ coding: utf-8 _*_ # __Author: "LEM ...

  2. 10 Minutes to pandas

    摘要   一.创建对象 二.查看数据 三.选择和设置 四.缺失值处理 五.相关操作 六.聚合 七.重排(Reshaping) 八.时间序列 九.Categorical类型   十.画图      十一 ...

  3. 利用Python进行数据分析(15) pandas基础: 字符串操作

      字符串对象方法 split()方法拆分字符串: strip()方法去掉空白符和换行符: split()结合strip()使用: "+"符号可以将多个字符串连接起来: join( ...

  4. 利用Python进行数据分析(10) pandas基础: 处理缺失数据

      数据不完整在数据分析的过程中很常见. pandas使用浮点值NaN表示浮点和非浮点数组里的缺失数据. pandas使用isnull()和notnull()函数来判断缺失情况. 对于缺失数据一般处理 ...

  5. 利用Python进行数据分析(12) pandas基础: 数据合并

    pandas 提供了三种主要方法可以对数据进行合并: pandas.merge()方法:数据库风格的合并: pandas.concat()方法:轴向连接,即沿着一条轴将多个对象堆叠到一起: 实例方法c ...

  6. 利用Python进行数据分析(9) pandas基础: 汇总统计和计算

    pandas 对象拥有一些常用的数学和统计方法.   例如,sum() 方法,进行列小计:   sum() 方法传入 axis=1 指定为横向汇总,即行小计:   idxmax() 获取最大值对应的索 ...

  7. 利用Python进行数据分析(8) pandas基础: Series和DataFrame的基本操作

    一.reindex() 方法:重新索引 针对 Series   重新索引指的是根据index参数重新进行排序. 如果传入的索引值在数据里不存在,则不会报错,而是添加缺失值的新行. 不想用缺失值,可以用 ...

  8. 利用Python进行数据分析(7) pandas基础: Series和DataFrame的简单介绍

    一.pandas 是什么 pandas 是基于 NumPy 的一个 Python 数据分析包,主要目的是为了数据分析.它提供了大量高级的数据结构和对数据处理的方法. pandas 有两个主要的数据结构 ...

  9. pandas.DataFrame对行和列求和及添加新行和列

    导入模块: from pandas import DataFrame import pandas as pd import numpy as np 生成DataFrame数据 df = DataFra ...

随机推荐

  1. Asp.Net之三层架构

    三层架构之理论: 通常意义上讲的三层架构就是将整个项目应用划分为:表现层(UI),业务逻辑层(BLL),数据访问层(DAL).与传统的二层架构的区别在于在用户界面(UI)和数据库服务器之间,添加中间层 ...

  2. iOS修改截取图片不规范问题

    +(UIImage *) imageCompressForWidth:(UIImage *)sourceImage targetWidth:(CGFloat)defineWidth{ UIImage ...

  3. js访问 xmldom

    加载XML文档:     var xmlDom = new ActiveXObject("MSXML2.DOMDocument");  xmlDom.load("file ...

  4. Description:一根高筋拉面,中间切一刀,可以得到2根面条。如果先对折1次,中间切一刀,可以得到3根面条。如果连续对折2次,中间切一刀,可以得到5根面条。Input:你的程序需要解决的问题是,输入连续对折的次数。NOutput输出中间切一刀,可以得到多少根面条。

    #include<iostream> using namespace std ; int main() { int n ; while(cin >> n) { << ...

  5. php7 install script

    ./configure --prefix=/home/admin/local/php7 --with-gd=/home/admin/local/libgd-2.1.1/ --with-jpeg-dir ...

  6. 利用border和伪类画出三角形 ps:好久没写博客了。。。

    有一个半月没有写博客了,这段时间,小哥我经历了自入行前端最为黑暗的时期,迷茫,空虚,不想写代码,不想做研究了.连打游戏都没有兴趣,如同行尸走肉一般.还好,毕业论文的初稿完成后,整个时间段最恶心最难熬的 ...

  7. git彻底删除commit记录的方法

    在github上,如果非默认分支的话,直接用以下方法: git reset --hard <commit_id> git push origin HEAD --force 如是默认分支,需 ...

  8. how to install git 1.8 rpm

    git版本在低于1.8之前,对于私有项目会出现401的pull失败错误,只能通过升级git版本来解决 It appears that git18 is no longer available from ...

  9. Makefile写法

    概述 -- 什么是makefile?或许很多Winodws的程序员都不知道这个东西,因为那些Windows的IDE都为你做了这个工作,但我觉得要作一个好的和professional的程序员,makef ...

  10. 引用(ajaxfileupload.js) ajaxfileupload.js报这错jQuery.handleError is not a function

    jQuery.handleError is not a function 原因是,经测试handlerError只在jquery-1.4.2之前的版本中存在,jquery-1.6 和1.7中都没有这个 ...