Pandas入门(二)——DataFrame结构及常用操作
2017-12-13 10:39
513 查看
继上一篇文章,这篇文章介绍一下Pandas模块里面的DataFrame结构
the master index of the DataFrame.
这段话的意思是,DataFrame提供的是一个类似表的结构,由多个Series组成,而Series在DataFrame中叫columns(理解有错请指出,(逃~
参数:
1、二维array;
2、Series 列表;
3、value为Series的字典;
2
3
4
5
6
7
2
3
4
5
6
7
2
3
4
5
6
7
注:若创建使用的参数中,array、Series长度不一样时,对应index的value值若不存在则为NaN
2
3
df.ix[条件,then操作区域]
2
3
np.where(条件,then,else)
2
3
2
3
(还有很多种方法就不一一列举了)
2
3
4
5
6
7
e.2 使用get_group 取出其中一分组
2
3
4
5
6
7
8
<原创文章,转载请注明出处>
1. 介绍
DataFrame unifies two or more Series into a single data structure.Each Series then represents a named column of the DataFrame, and instead of each column having its own index, the DataFrame provides a single index and the data in all columns is aligned tothe master index of the DataFrame.
这段话的意思是,DataFrame提供的是一个类似表的结构,由多个Series组成,而Series在DataFrame中叫columns(理解有错请指出,(逃~
2. 相关操作
a.create
pd.DataFrame()参数:
1、二维array;
2、Series 列表;
3、value为Series的字典;
a.1、二维array
import pandas as pd import numpy as np s1=np.array([1,2,3,4]) s2=np.array([5,6,7,8]) df=pd.DataFrame([s1,s2]) print df1
2
3
4
5
6
7
a.2、Series列表(效果与二维array相同)
import pandas as pd import numpy as np s1=pd.Series(np.array([1,2,3,4])) s2=pd.Series(np.array([5,6,7,8])) df=pd.DataFrame([s1,s2]) print df1
2
3
4
5
6
7
a.3、value为Series的字典结构;
import pandas as pd import numpy as np s1=pd.Series(np.array([1,2,3,4])) s2=pd.Series(np.array([5,6,7,8])) df=pd.DataFrame({"a":s1,"b":s2}); print df1
2
3
4
5
6
7
注:若创建使用的参数中,array、Series长度不一样时,对应index的value值若不存在则为NaN
b.属性
b.1 .columns :每个columns对应的keys
b.2 .shape:形状,(a,b),index长度为a,columns数为b
b.3 .index;.values:返回index列表;返回value二维array
b.4 .head();.tail();
c.if-then 操作
c.1使用.ix[]
df=pd.DataFrame({"A":[1,2,3,4],"B":[5,6,7,8],"C":[1,1,1,1]}) df.ix[df.A>1,'B']= -1 print df1
2
3
df.ix[条件,then操作区域]
c.2使用numpy.where
df=pd.DataFrame({"A":[1,2,3,4],"B":[5,6,7,8],"C":[1,1,1,1]}) df["then"]=np.where(df.A<3,1,0) print df1
2
3
np.where(条件,then,else)
d.根据条件选择取DataFrame
d.1 直接取值df.[]
df=pd.DataFrame({"A":[1,2,3,4],"B":[5,6,7,8],"C":[1,1,1,1]}) df=df[df.A>=2] print df1
2
3
d.2 使用.loc[]
df=pd.DataFrame({"A":[1,2,3,4],"B"< 4000 /span>:[5,6,7,8],"C":[1,1,1,1]}) df=df.loc[df.A>2] print df1
2
3
(还有很多种方法就不一一列举了)
e.Grouping
e.1groupby 形成group
df = pd.DataFrame({'animal': 'cat dog cat fish dog cat cat'.split(), 'size': list('SSMMMLL'), 'weight': [8, 10, 11, 1, 20, 12, 12], 'adult' : [False] * 5 + [True] * 2}); #列出动物中weight最大的对应size group=df.groupby("animal").apply(lambda subf: subf['size'][subf['weight'].idxmax()]) print group1
2
3
4
5
6
7
e.2 使用get_group 取出其中一分组
df = pd.DataFrame({'animal': 'cat dog cat fish dog cat cat'.split(), 'size': list('SSMMMLL'), 'weight': [8, 10, 11, 1, 20, 12, 12], 'adult' : [False] * 5 + [True] * 2}); group=df.groupby("animal") cat=group.get_group("cat") print cat1
2
3
4
5
6
7
8
其他具体操作请参考CookBook
http://pandas.pydata.org/pandas-docs/stable/cookbook.html<原创文章,转载请注明出处>
相关文章推荐
- Pandas入门(二)——DataFrame结构及常用操作
- Pandas入门(二)——DataFrame结构及常用操作
- python:pandas模块中的DataFrame结构及常用操作
- python_pandas_dataframe_行列选择_切片操作
- pandas常用操作
- 开发环境入门 linux基础 基本操作命令(部分) 文本结构和基本命令
- Scala入门到精通—— 第二节Scala基本类型及操作、程序控制结构
- DataGrip使用入门-常用操作(三)
- Linux 操作入门--常用命令记录
- GDI+常用操作、入门解析
- git常用操作以及快速入门教程
- IntelliJ IDEA 入门常用操作
- Ext Js入门第5篇-DOM元素常用操作
- Redis入门之五大数据类型及常用操作
- MongoDB入门教程(包含安装、常用命令、相关概念、使用技巧、常见操作等)
- Pandas常用操作
- git(一)-git入门,版本库初始化以及常用命令操作
- 70个shell常用操作、 shell编程控制结构:expr、let、for、while、until、shift、if、case、break、continue、函数、select
- 数据结构之顺序表常用操作整理
- dataframe进行常用统计、分组统计平均绝对偏差等操作函数。