Pandas簡單使用1

  • Pandas 基本介紹
  • Pandas選擇數據
  • Pandas設置值

Pandas基本介紹

  • Numpy是列表形式的,沒有數值標籤,而Pandas是字典形式。Pandas是基於Numpy構建的,讓Numpy爲中心的應用變得更加簡單。
  • Pandas主要有兩個數據結構,Series和DataFrame。

Series

import pandas as pd
import numpy as np
s = pd.Series([1,3,6,np.nan,44,1])
print(s)
print(s[1])   #可以直接訪問

Series的字符串表現形式爲:索引在左邊,值在右邊。由於沒有指定索引,默認創建0到N-1的整數型索引。下面是加上索引的Series

grade = pd.Series([100,59,80],index=["李明","李紅","王美"])
print(grade.values)
print(grade.index)
print(grade["李明"])

DataFrame

dates = pd.date_range("20160101",periods=6)
df = pd.DataFrame(np.random.randn(6,4),index = dates,columns=['a','b','c','d'])  
print(df)

                   a         b         c         d
2016-01-01 -0.378199 -0.300236 -1.207843 -1.658223
2016-01-02 -1.031397 -0.834695 -0.417703 -0.318720
2016-01-03 -2.346667  1.615651  1.726296  1.152253
2016-01-04  1.389872  0.952453 -0.737092  1.555059
2016-01-05  0.735490  0.297005 -0.542341  0.559540
2016-01-06 -1.962791  1.776028 -1.917368 -0.679542

DataFrame是一個表格型的數據結構,它包含有一組有序的列,每列可以是不同的值類型(數值、字符串、布爾值等)。
DataFrame既有行索引也有列索引,它可以被看作由Series組成的大字典。

下面訪問DataFrame中的數據,注意訪問具體元素是先列標籤後行標籤

print(df['b'])

2016-01-01   -0.300236
2016-01-02   -0.834695
2016-01-03    1.615651
2016-01-04    0.952453
2016-01-05    0.297005
2016-01-06    1.776028
Freq: D, Name: b, dtype: float64

print(df['b']['2016-01-05'])

0.2970052798746942

創建一組沒有給定行標籤和列標籤的數據並訪問

df = pd.DataFrame(np.arange(12).reshape((3,4)))
print(df)
print(df[1][0])
   0  1   2   3
0  0  1   2   3
1  4  5   6   7
2  8  9  10  11
1

自定義每列的類型

df1 = pd.DataFrame({
    'A':1,
    'B':pd.Timestamp('20180928'),
    'C':pd.Series(1,index=list(range(4)),dtype='float32'),
    'D':np.array([3]*4,dtype='int32'),
    'E':pd.Categorical(["test","train","test","train"]),
    'F':'foo'})
print(df1)
print(df1['B'])
print(df1['B'][1])

   A          B    C  D      E    F
0  1 2018-09-28  1.0  3   test  foo
1  1 2018-09-28  1.0  3  train  foo
2  1 2018-09-28  1.0  3   test  foo
3  1 2018-09-28  1.0  3  train  foo
0   2018-09-28
1   2018-09-28
2   2018-09-28
3   2018-09-28
Name: B, dtype: datetime64[ns]
2018-09-28 00:00:00

查看每行的名稱

print(df1.index)

Int64Index([0, 1, 2, 3], dtype='int64')

查看每列的名稱

print(df1.columns)
Index(['A', 'B', 'C', 'D', 'E', 'F'], dtype='object')

查看所有的值

print(df1.values)

[[1 Timestamp('2018-09-28 00:00:00') 1.0 3 'test' 'foo']
 [1 Timestamp('2018-09-28 00:00:00') 1.0 3 'train' 'foo']
 [1 Timestamp('2018-09-28 00:00:00') 1.0 3 'test' 'foo']
 [1 Timestamp('2018-09-28 00:00:00') 1.0 3 'train' 'foo']]

查看數據總結

print(df1.describe())

         A    C    D
count  4.0  4.0  4.0
mean   1.0  1.0  3.0
std    0.0  0.0  0.0
min    1.0  1.0  3.0
25%    1.0  1.0  3.0
50%    1.0  1.0  3.0
75%    1.0  1.0  3.0
max    1.0  1.0  3.0

對數據的index進行排序輸出

print(df1.sort_index(axis=1,ascending=False))
     F      E  D    C          B  A
0  foo   test  3  1.0 2018-09-28  1
1  foo  train  3  1.0 2018-09-28  1
2  foo   test  3  1.0 2018-09-28  1
3  foo  train  3  1.0 2018-09-28  1

對數據的value進行排序輸出

print(df1.sort_values(by='B'))
   A          B    C  D      E    F
0  1 2018-09-28  1.0  3   test  foo
1  1 2018-09-28  1.0  3  train  foo
2  1 2018-09-28  1.0  3   test  foo
3  1 2018-09-28  1.0  3  train  foo

Pandas選擇數據

簡單篩選

print(df)
            A   B   C   D
2013-01-01   0   1   2   3
2013-01-02   4   5   6   7
2013-01-03   8   9  10  11
2013-01-04  12  13  14  15
2013-01-05  16  17  18  19
2013-01-06  20  21  22  23

print(df['A'])
2013-01-01     0
2013-01-02     4
2013-01-03     8
2013-01-04    12
2013-01-05    16
2013-01-06    20
Freq: D, Name: A, dtype: int64

print(df.A)
2013-01-01     0
2013-01-02     4
2013-01-03     8
2013-01-04    12
2013-01-05    16
2013-01-06    20
Freq: D, Name: A, dtype: int64

print(df[0:3])
            A  B   C   D
2013-01-01  0  1   2   3
2013-01-02  4  5   6   7
2013-01-03  8  9  10  11

print(df['20130102':'20130104'])
             A   B   C   D
2013-01-02   4   5   6   7
2013-01-03   8   9  10  11
2013-01-04  12  13  14  15

標籤 loc 選擇

print(df.loc['20130102'])
A    4
B    5
C    6
D    7
Name: 2013-01-02 00:00:00, dtype: int64

print(df.loc[:,['A','B']])
             A   B
2013-01-01   0   1
2013-01-02   4   5
2013-01-03   8   9
2013-01-04  12  13
2013-01-05  16  17
2013-01-06  20  21

print(df.loc['20130102',['A','B']])
A    4
B    5
Name: 2013-01-02 00:00:00, dtype: int64

序列 iloc 選擇

print(df.iloc[3,1])
13

print(df.iloc[3:5,1:3])
             B   C
2013-01-04  13  14
2013-01-05  17  18

print(df.iloc[[1,3,5],1:3])
             B   C
2013-01-02   5   6
2013-01-04  13  14
2013-01-06  21  22

混合兩種 ix 選擇

print(df.ix[:3,['A','C']])   #混合選擇
            A   C
2013-01-01  0   2
2013-01-02  4   6
2013-01-03  8  10

通過判斷的篩選

print(df[df.A>8])
             A   B   C   D
2013-01-04  12  13  14  15
2013-01-05  16  17  18  19
2013-01-06  20  21  22  23

Pandas設置值


#Pandas設置值
dates = pd.date_range('20180901',periods=6)
df = pd.DataFrame(np.arange(24).reshape((6,4)),index=dates,columns=['A','B','C','D'])
print(df)
             A   B   C   D
2018-09-01   0   1   2   3
2018-09-02   4   5   6   7
2018-09-03   8   9  10  11
2018-09-04  12  13  14  15
2018-09-05  16  17  18  19
2018-09-06  20  21  22  23

#根據位置設置loc和iloc
df.loc['20180903','B'] = 100
df.iloc[5,3] = 200
print(df)
             A    B   C    D
2018-09-01   0    1   2    3
2018-09-02   4    5   6    7
2018-09-03   8  100  10   11
2018-09-04  12   13  14   15
2018-09-05  16   17  18   19
2018-09-06  20   21  22  200


#根據條件設置
df.B[df.A>9] = 0
print(df)
             A    B   C    D
2018-09-01   0    1   2    3
2018-09-02   4    5   6    7
2018-09-03   8  100  10   11
2018-09-04  12    0  14   15
2018-09-05  16    0  18   19
2018-09-06  20    0  22  200

#按行或列設置
df['F'] = 0
print(df)
             A    B   C    D  F
2018-09-01   0    1   2    3  0
2018-09-02   4    5   6    7  0
2018-09-03   8  100  10   11  0
2018-09-04  12    0  14   15  0
2018-09-05  16    0  18   19  0
2018-09-06  20    0  22  200  0

#添加數據
df['E'] = pd.Series([1,2,3,4,5,6],index = dates)
print(df)
             A    B   C    D  F  E
2018-09-01   0    1   2    3  0  1
2018-09-02   4    5   6    7  0  2
2018-09-03   8  100  10   11  0  3
2018-09-04  12    0  14   15  0  4
2018-09-05  16    0  18   19  0  5
2018-09-06  20    0  22  200  0  6

文章轉載於莫煩Python

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章