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, ...
随机推荐
- Scala入门4(_的用法)
从网上找了一篇博客,详细讲解了Scala下划线的用法,这里做保留 博客链接
- Wannafly挑战赛9 C - 列一列
链接:https://www.nowcoder.com/acm/contest/71/C来源:牛客网 题目描述 小W在计算一个数列{An},其中A1=1,A2=2,An+2=An+1+An.尽管他计算 ...
- POJ - 3111 K Best 0-1分数规划 二分
K Best Time Limit: 8000MS Memory Limit: 65536K Total Submissions: 12812 Accepted: 3290 Case Time ...
- List元素为泛型时的注意事项
最近的项目赶得非常紧,这节奏跟最近的天气一点也不搭调. 编码的过程,遇到一个关于List的小问题. 在调用List.add(E e)的时候范了一个小毛病,很自然地认为list中存储的是 E 对象的另 ...
- python修改文件的属性
1.执行attrib系统命令 ATTRIB [+R | -R] [+A | -A ] [+S | -S] [+H | -H] [+I | -I] [drive:][path][filename] [/ ...
- python开发_platform_获取操作系统详细信息工具
''' python中,platform模块给我们提供了很多方法去获取操作系统的信息 如: import platform platform.platform() #获取操作系统名称及版本号,'Win ...
- Codeforces Beta Round #37 B. Computer Game 暴力 贪心
B. Computer Game 题目连接: http://www.codeforces.com/contest/37/problem/B Description Vasya's elder brot ...
- 机器学习(4):BP神经网络原理及其python实现
BP神经网络是深度学习的重要基础,它是深度学习的重要前行算法之一,因此理解BP神经网络原理以及实现技巧非常有必要.接下来,我们对原理和实现展开讨论. 1.原理 有空再慢慢补上,请先参考老外一篇不错的 ...
- 说说最小生成树(Minimum Spanning Tree)
minimum spanning tree(MST) 最小生成树是连通无向带权图的一个子图,要求 能够连接图中的所有顶点.无环.路径的权重和为所有路径中最小的. graph-cut 对图的一个切割或者 ...
- oracle 取整的几种方法
--1.取整(大) select ceil(-1.001) value from dual ; --2.取整(小) select floor(-1.001) value from dual ...