1. 程式人生 > >利用python數據分析panda學習筆記之基本功能

利用python數據分析panda學習筆記之基本功能

數據分析 method 入行 整數 -s cnblogs 3.4 style fill

1 重新生成索引 如果某個索引值不存在就引入缺失值

1 from pandas import Series,DataFrame
2 import pandas as pd
3 import numpy as np
4 obj=Series([4.5,7.2,-5.3,3.6],index=[d,b,a,c])
5 obj
6 
7 #重新生成索引
8 obj2=obj.reindex([a,b,c,d,e])
9 obj2

技術分享

技術分享

  a使用method的ffill可以實現前向值填充,效果如下

1 #前向填充
2 obj3=Series([blue
,purple,yellow],index=[0,2,4]) 3 obj3.reindex(range(6),method=ffill)

技術分享

技術分享

  b:對於dataframe使用reindex可以同時修改行列索引,如果僅傳入一個序列那麽如下

1 frame=DataFrame(np.arange(9).reshape((3,3)),index=[a,c,d],
2                 columns=[ohio,Texas,california])
3 frame

技術分享

1 frame2=frame.reindex([a,b,c,d
]) 2 frame2

技術分享

  c:使用colunms重新索引列

1 states=[Texax,Utah,california]
2 frame.reindex(columns=states)

技術分享

  d:同時插入行列,但是插值只能按行應用

1 #同時對行 列進行重新索引 而插值只能引用到行
2 frame.reindex(index=[a,b,c,d],method=ffill,
3               columns=states)

技術分享

reindex的參數說明如下:

技術分享

2 丟棄制定軸上的項

  a:drop方法返回一個指定軸上刪除了指定值的新對象,刪除列c

1 #丟棄指定軸的項
2 obj=Series(np.arange(5.),index=[a,b,c,d,e])
3 new_obj=obj.drop(c)
4 new_obj

技術分享

  b:刪除兩個 b c

obj.drop([d,c])

技術分享

  c:對於dataframe可以刪除任意軸上的索引

1 #對於DataFrame可以刪除任意軸的索引
2 data = DataFrame(np.arange(16).reshape((4,4)),
3                  index=[ohio,colorado,utah,new york],
4                    columns=[one,two,three,four])
5 #刪除兩個
6 data.drop([colorado,ohio])

技術分享

3 索引,選取和過濾

  a:Series中的索引類似與Numpy,但是不只是整數,索引字符

1 obj=Series(np.arange(4.),index=[a,b,c,d])
2 obj[b]#1.0

技術分享

技術分享

  b:按照整數,範圍

1 obj[1]#1.0
2 obj[2:4]# 2 3

技術分享

  c:利用標簽的切片運算和普通depython切片不同,其包含末端

1 obj[b:c]#b c 1 2

技術分享

  d:那麽對dataframe進行索引就是獲取一個或者多個列勒

1 data=DataFrame(np.arange(16).reshape(4,4),
2            index=[ohio,colorado,mike,jason],
3                  columns=[one,two,three,four])
4 data

技術分享

  e:選擇一列

data[two]#輸出第二列+行號 也就是索引

技術分享

  f:選擇多列

1 data[[three,one]]

技術分享

  g:選取行標簽前兩行

data[:2]#選取的是前面兩行

技術分享

  h:選取第三列大於5的值

data[data[three]>5]

技術分享

  i:為了能在dataframe的行上進行標簽索引引入字段ix

data.ix[colorado,[two,three]]

技術分享

  j:選取第4 1 2列 而且行為colorado jason

data.ix[[colorado,jason],[3,0,1]]

技術分享

  k:輸出行mike

data.ix[2]

技術分享

DataFrame索引總結

技術分享

4 算數運算和數據對齊

  a:Series的加法

1 s1=Series([7.3,-2.5,3.4,1.5],index=[a,c,d,e])
2 s2=Series([-2.1,3.6,-1.5,4,3.1],index=[a,c,e,f,g])
3 s1+s2

技術分享技術分享

技術分享

  b:對於dataframe,對齊會同時發生在行 列中

df1=DataFrame(np.arange(9.).reshape((3,3)),columns=list(bcd),
              index=[utah,ohio,colorado])
df2=DataFrame(np.arange(12.).reshape((4,3)),columns=list(bde),
              index=[utah,ohio,colorado,oragen])

技術分享

技術分享

df1+df2

技術分享

------>索引和列都為其並集

  c:在算術方法中填充值。比如說兩個dataframe相加,其中一個不在的時候填充為0

1 #算術中進行填充
2 df1=DataFrame(np.arange(12.).reshape((3,4)),columns=list(abcd))
3 df2=DataFrame(np.arange(20.).reshape((4,5)),columns=list(abcde))
4 df1+df2

技術分享

#使用df1的add方法 傳入df2以及一個fill_value參數
df1.add(df2,fill_value=0)

技術分享

5 DataFrame和Series之間的運算----->廣播,也就是如果第一個數值-1,那麽這個列都會減1

  a:看一看一個二維數組和一行之間的差

arr=np.arange(12.).reshape((3,4))

技術分享

arr[0]

技術分享

arr-arr[0]

技術分享

  b:frame和series的運算

frame=DataFrame(np.arange(12.).reshape((4,3)),columns=list(bde),
                index=[utah,ohio,texas,orogen])
series=frame.ix[0]

技術分享

技術分享

frame-series

技術分享

好了,加油騷年!!!!

利用python數據分析panda學習筆記之基本功能