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

  1. df.loc[0, 'a']  
  2. df.loc[0:3, ['a', 'b']]  
  3. df.loc[[1, 5], ['b', 'c']]  

由于这边我们没有命名index,所以是DataFrame自动赋予的,为数字0-9

二、如果我们嫌column name太长了,输入不方便,有或者index是一列时间序列,更不好输入,那就可以选择 .iloc了。这边的 i 我觉得代表index,比较好记点。

  1. df.iloc[1,1]  
  2. df.iloc[0:3, [0,1]]  
  3. 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可以还原索引,从新变为默认的整型索引。

猜你喜欢

转载自www.cnblogs.com/ranjiewen/p/9110985.html
今日推荐