python pandas.Series&&DataFrame&& set_index&reset_index
Pandas模块是Python用于数据导入及整理的模块,对数据挖掘前期数据的处理工作十分有用,因此这些基础的东西还是要好好的学学。Pandas模块的数据结构主要有两:1、Series ;2、DataFrame
先了解一下Series结构。
a.创建
a.1、pd.Series([list],index=[list])//以list为参数,参数为一list;index为可选参数,若不填则默认index从0开始;若添则index长度与value长度相等
import pandas as pd s=pd.Series([1,2,3,4,5],index= ['a','b','c','f','e']) print(s) a 1
b 2
c 3
f 4
e 5
dtype: int64 s=pd.Series({'a':3,'b':4,'c':5,'f':6,'e':8}) print(s) a 3
b 4
c 5
e 8
f 6
dtype: int64 import numpy as np v=np.random.random_sample(50) s=pd.Series(v) print (s.head()) print (s.tail(3)) 0 0.785486
1 0.272487
2 0.182683
3 0.196650
4 0.654694
dtype: float64
47 0.701705
48 0.897344
49 0.478941
dtype: float64
Series相当于数组numpy.array类似
pandas中的isnull和notnull函数可以用于检测缺失数据
Series最重要的一个功能是:它在算术运算中会自动对齐不同索引的数据。
Series对象本身及其索引都有一个name属性,该属性跟pandas其他的关键功能关系非常密切
DataFrame相当于有表格,有行表头和列表头
a=pd.DataFrame(np.random.rand(4,5),index=list("ABCD"),columns=list('abcde'))
print (a)
a b c d e
A 0.484914 0.256542 0.702622 0.997324 0.834293
B 0.802564 0.660622 0.246160 0.936310 0.841891
C 0.073188 0.369238 0.631770 0.967714 0.950021
D 0.136728 0.270609 0.102326 0.343002 0.789243
#增加列或修改列
a['f']=[1,2,3,4]
a['e']=10
print(a)
a b c d e f
A 0.484914 0.256542 0.702622 0.997324 10 1
B 0.802564 0.660622 0.246160 0.936310 10 2
C 0.073188 0.369238 0.631770 0.967714 10 3
D 0.136728 0.270609 0.102326 0.343002 10 4
#增加行或修改行
a.ix['D']=10
print(a)
a b c d e f
A 0.484914 0.256542 0.702622 0.997324 10 1
B 0.802564 0.660622 0.246160 0.936310 10 2
C 0.073188 0.369238 0.631770 0.967714 10 3
D 10.000000 10.000000 10.000000 10.000000 10 10
E:\Program Files\Anaconda3\envs\tensorflow_py35\lib\site-packages\ipykernel\__main__.py:2: DeprecationWarning:
.ix is deprecated. Please use
.loc for label based indexing or
.iloc for positional indexing
See the documentation here:
http://pandas.pydata.org/pandas-docs/stable/indexing.html#ix-indexer-is-deprecated
from ipykernel import kernelapp as app
print (a[['b','e']]) #取'b','e'列
print (a.loc['A':'D',['a','c','f']]) #取'A'-'D'行'a','c','f'列
b e
A 0.256542 10
B 0.660622 10
C 0.369238 10
D 10.000000 10
a c f
A 0.484914 0.702622 1
B 0.802564 0.246160 2
C 0.073188 0.631770 3
D 10.000000 10.000000 10
#减少行或减少列
a=a.drop(['C','D']) #删除'C'行和'D'
print (a)
a=a.drop('a',axis=1) #删除'a'列,axis=0表示行,axis=1表示列
print(a)
a b c d e f
A 0.484914 0.256542 0.702622 0.997324 10 1
B 0.802564 0.660622 0.246160 0.936310 10 2
b c d e f
A 0.256542 0.702622 0.997324 10 1
B 0.660622 0.246160 0.936310 10 2
#缺省值处理
a=pd.DataFrame(np.random.rand(4,6),index=list('EFGH'),columns=list('abcdef'))
print(a)
a.iloc[2,3]=None #取第三行第4列值设为None
a.iloc[3,0]=None #取第五行第1列值设为None
print(a)
a b c d e f
E 0.559810 0.470429 0.966709 0.096261 0.220432 0.878908
F 0.567841 0.237288 0.117921 0.604651 0.055591 0.272852
G 0.267982 0.053754 0.410986 0.310045 0.058950 0.773051
H 0.595787 0.932286 0.839897 0.757793 0.554378 0.417178
a b c d e f
E 0.559810 0.470429 0.966709 0.096261 0.220432 0.878908
F 0.567841 0.237288 0.117921 0.604651 0.055591 0.272852
G 0.267982 0.053754 0.410986 NaN 0.058950 0.773051
H NaN 0.932286 0.839897 0.757793 0.554378 0.417178
#缺省值处理
a=a.fillna(5) #缺省值处(即NaN处填充为5)
print (a)
#缺省值去行即有缺省值的把这一行都去掉
a.iloc[2,3]=None
a.iloc[3,0]=None
print (a)
a=a.dropna() #删除缺省值为NaN的行
print (a)
a b c d e f
E 0.559810 0.470429 0.966709 0.096261 0.220432 0.878908
F 0.567841 0.237288 0.117921 0.604651 0.055591 0.272852
G 0.267982 0.053754 0.410986 5.000000 0.058950 0.773051
H 5.000000 0.932286 0.839897 0.757793 0.554378 0.417178
a b c d e f
E 0.559810 0.470429 0.966709 0.096261 0.220432 0.878908
F 0.567841 0.237288 0.117921 0.604651 0.055591 0.272852
G 0.267982 0.053754 0.410986 NaN 0.058950 0.773051
H NaN 0.932286 0.839897 0.757793 0.554378 0.417178
a b c d e f
E 0.559810 0.470429 0.966709 0.096261 0.220432 0.878908
F 0.567841 0.237288 0.117921 0.604651 0.055591 0.272852
python pandas.DataFrame选取、修改数据最好用.loc,.iloc,.ix :
那么这三种选取数据的方式该怎么选择呢?
一、当每列已有column name时,用 df [ 'a' ] 就能选取出一整列数据。如果你知道column names 和index,且两者都很好输入,可以选择 .loc
- df.loc[0, 'a']
- df.loc[0:3, ['a', 'b']]
- df.loc[[1, 5], ['b', 'c']]
由于这边我们没有命名index,所以是DataFrame自动赋予的,为数字0-9
二、如果我们嫌column name太长了,输入不方便,有或者index是一列时间序列,更不好输入,那就可以选择 .iloc了。这边的 i 我觉得代表index,比较好记点。
- df.iloc[1,1]
- df.iloc[0:3, [0,1]]
- df.iloc[[0, 3, 5], 0:2]
iloc 使得我们可以对column使用slice(切片)的方法对数据进行选取。
三、.ix 的功能就更强大了,它允许我们混合使用下标和名称进行选取。 可以说它涵盖了前面所有的用法。基本上把前面的都换成df.ix 都能成功,但是有一点,就是df.ix [ [ ..1.. ], [..2..] ], 1框内必须统一,必须同时是下标或者名称
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 (df)
A B C
0 1 5 1
1 2 -1 1
2 3 -1 1
3 4 -1 1
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 (df)
A B C then
0 1 5 1 1
1 2 6 1 1
2 3 7 1 0
3 4 8 1 0
df=pd.DataFrame({"A":[1,2,3,4],"B":[5,6,7,8],"C":[1,1,1,1]})
df=df.loc[df.A>2]
print (df)
A B C
2 3 7 1
3 4 8 1
DataFrame可以通过set_index方法,可以设置单索引和复合索引。
reset_index可以还原索引,从新变为默认的整型索引。
python pandas.Series&&DataFrame&& set_index&reset_index的更多相关文章
- python. pandas(series,dataframe,index) method test
python. pandas(series,dataframe,index,reindex,csv file read and write) method test import pandas as ...
- python pandas ---Series,DataFrame 创建方法,操作运算操作(赋值,sort,get,del,pop,insert,+,-,*,/)
pandas 是基于 Numpy 构建的含有更高级数据结构和工具的数据分析包 pandas 也是围绕着 Series 和 DataFrame 两个核心数据结构展开的, 导入如下: from panda ...
- Python Pandas -- Series
pandas.Series class pandas.Series(data=None, index=None, dtype=None, name=None, copy=False, fastpath ...
- python基础:如何使用python pandas将DataFrame转换为dict
之前在知乎上看到有网友提问,如何将DataFrame转换为dict,专门研究了一下,pandas在0.21.0版本中是提供了这个方法的.下面一起学习一下,通过调用help方法,该方法只需传入一个参数, ...
- pandas数据结构:Series/DataFrame;python函数:range/arange
1. Series Series 是一个类数组的数据结构,同时带有标签(lable)或者说索引(index). 1.1 下边生成一个最简单的Series对象,因为没有给Series指定索引,所以此时会 ...
- 利用Python进行数据分析:【Pandas】(Series+DataFrame)
一.pandas简单介绍 1.pandas是一个强大的Python数据分析的工具包.2.pandas是基于NumPy构建的.3.pandas的主要功能 --具备对其功能的数据结构DataFrame.S ...
- python 数据处理学习pandas之DataFrame
请原谅没有一次写完,本文是自己学习过程中的记录,完善pandas的学习知识,对于现有网上资料的缺少和利用python进行数据分析这本书部分知识的过时,只好以记录的形势来写这篇文章.最如果后续工作定下来 ...
- 【跟着stackoverflow学Pandas】 - Adding new column to existing DataFrame in Python pandas - Pandas 添加列
最近做一个系列博客,跟着stackoverflow学Pandas. 以 pandas作为关键词,在stackoverflow中进行搜索,随后安照 votes 数目进行排序: https://stack ...
- pandas set_index() reset_index()
set_index() 官方定义: 使用一个或多个现有列设置索引, 默认情况下生成一个新对象 DataFrame.set_index(keys, drop=True, append=False, ...
随机推荐
- javacript 实现两个数组的差集
<script type="text/javascript"> var array1 = [1,2,3,4,5,6,7,8,9]; var arra ...
- 我们不能把JavaScript作为一个全功能的编程语言。它缺乏以下重要特征
客户端JavaScript不允许读或写文件.这已被保存的安全原因. JavaScript不能用于网络的应用,因为没有这样的支持. JavaScript没有任何多线程或多处理器的能力.
- android 定时, 延时 任务
韩梦飞沙 韩亚飞 313134555@qq.com yue31313 han_meng_fei_sha 倒计时类 用 倒计时定时器CountDownTimer 延迟类 CountDownT ...
- [BZOJ4567][SCOI2016]背单词(Trie+贪心)
1.题意表述十分难以理解,简单说就是:有n个单词,确定一个背的顺序,使总代价最小. 2.因为第(1)种情况的代价是n*n,这个代价比任何一种不出现第(1)种情况的方案都要大,所以最后肯定不会出现“背某 ...
- Linux下rz,sz与ssh的配合使用,实现文件传输
一般来说,linux服务器大多是通过ssh客户端来进行远程的登陆和管理的,使用ssh登陆linux主机以后,如何能够快速的和本地机器进行文件的交互呢,也就是上传和下载文件到服务器和本地: 与ss ...
- web文件上传组件比较jQuery File Upload和Fine Uploader
jQuery File Upload: https://blueimp.github.io/jQuery-File-Upload/ Fine Uploader: http://fineuploader ...
- CF 277.5 A.SwapSort 水题
//STL教你做人系列 #include<stdio.h> #include<iostream> #include<math.h> #include<algo ...
- oracle开发学习篇之集合函数
集合函数; declare type list_nested ) not null; v_all list_nested := list_nested('changan','hubei','shang ...
- Centos6.5下Oracle 11g R2安装过程
1准备 CentOS-6.5-x86_64-bin-DVD1 linux_11gR2_database_1of2 linux_11gR2_database_2of2 VMware Workstatio ...
- [self removeAllSubviews]; // 删除所有子视图(包括 selfl.view) 所以,要慎用
[self removeAllSubviews]; //删除所有子视图(包括 selfl.view) 所以,要慎用