import pandas as pd import numpy as np
s = pd.Series([1, 2, 3, 4, 5, np.nan, 6, 7]) print(s) # 0 1.0 # 1 2.0 # 2 3.0 # 3 4.0 # 4 5.0 # 5 NaN # 6 6.0 # 7 7.0 # dtype: float64
dates = pd.date_range('20190101', periods=6) # index行名,columns列名 df = pd.DataFrame(np.random.randn(6, 4), index=dates, columns=['a', 'b', 'c', 'd']) print('列選取'.center(50, '-')) print(df) # a b c d # 2019-01-01 -1.294464 0.706790 -0.164825 -0.237432 # 2019-01-02 -1.091822 0.824446 0.748465 -0.191267 # 2019-01-03 -0.755218 1.637604 -1.896371 -0.093815 # 2019-01-04 -2.610031 -0.705783 -1.247235 -1.398978 # 2019-01-05 -0.324550 1.014212 1.375527 -0.409117 # 2019-01-06 -0.512911 0.301417 1.227190 0.771551 # 按照列選擇 print('列選取'.center(50, '-')) print(df['c']) # 2019-01-01 -0.164825 # 2019-01-02 0.748465 # 2019-01-03 -1.896371 # 2019-01-04 -1.247235 # 2019-01-05 1.375527 # 2019-01-06 1.227190 # Freq: D, Name: c, dtype: float64
df_1 = pd.DataFrame({ 'A': 1, # 直接賦值,若是index數量大於1,默認此列按照這個值重複 'B': pd.Timestamp('20190930'), # 直接賦值Timestamp格式,多行,重複這個值 'C': pd.Series(2, index=list(range(4)), dtype='float'), # Series結構使用index,定義出整個函數的值 'D': pd.Categorical([1, 2, 3, 4]), 'E': ['a', 'b', 'c', 'd'], 'F': 'beer', 'G': [1, 5, 4, 4] }) print(df_1) # A B C D E F G # 0 1 2019-09-30 2.0 1 a beer 1 # 1 1 2019-09-30 2.0 2 b beer 5 # 2 1 2019-09-30 2.0 3 c beer 4 # 3 1 2019-09-30 2.0 4 d beer 4
print('types'.center(50, '-')) print(df_1.dtypes) # 按列列出每列的數據類型 # A int64 # B datetime64[ns] # C float64 # D category # E object # F object # G int64 # dtype: object print('index'.center(50, '-')) print(df_1.index, type(df_1.index)) # Int64Index([0, 1, 2, 3], dtype='int64') # Int64Index([0, 1, 2, 3], dtype='int64') <class 'pandas.core.indexes.numeric.Int64Index'> print('columns'.center(50, '-')) print(df_1.columns, type(df_1.columns)) # Index(['A', 'B', 'C', 'D', 'E', 'F'], dtype='object') # Index(['A', 'B', 'C', 'D', 'E', 'F', 'G'], dtype='object') <class 'pandas.core.indexes.base.Index'> print('values'.center(50, '-')) print(df_1.values, type(df_1.values)) # 打印值,結果未numpy結構 # [[1 Timestamp('2019-09-30 00:00:00') 2.0 1 'a' 'beer' 1] # [1 Timestamp('2019-09-30 00:00:00') 2.0 2 'b' 'beer' 5] # [1 Timestamp('2019-09-30 00:00:00') 2.0 3 'c' 'beer' 4] # [1 Timestamp('2019-09-30 00:00:00') 2.0 4 'd' 'beer' 4]] <class 'numpy.ndarray'> print('describe'.center(50, '-')) print(df_1.describe()) # 打印值,結果爲numpy結構 # A C G # count 4.0 4.0 4.000000 計數 # mean 1.0 2.0 2.500000 平均數 # std 0.0 0.0 1.290994 方差 # min 1.0 2.0 1.000000 最小值 # 25% 1.0 2.0 1.750000 第一分位數 # 50% 1.0 2.0 2.500000 第二分位數 # 75% 1.0 2.0 3.250000 第三分位數 # max 1.0 2.0 4.000000 最大值 print('數據翻轉'.center(50, '-')) print(df_1.T) # 打印值,結果未numpy結構 # 0 ... 3 # # A 1 ... 1 # # B 2019-09-30 00:00:00 ... 2019-09-30 00:00:00 # # C 2 ... 2 # # D 1 ... 4 # # E a ... d # # F beer ... beer # # G 1 ... 4 # # [7 rows x 4 columns] print('數據按照列排序'.center(50, '-')) # axis=1,按照行進行排序 # axis=0,按照列進行排序 # ascending=False倒序輸出,True爲正序輸出 print('按照index排序'.center(50, '-')) print(df_1.sort_index(axis=0, ascending=False)) # A B C D E F G # 3 1 2019-09-30 2.0 4 d beer 4 # 2 1 2019-09-30 2.0 3 c beer 4 # 1 1 2019-09-30 2.0 2 b beer 5 # 0 1 2019-09-30 2.0 1 a beer 1 print('G列排序'.center(50, '-')) print(df_1.sort_values(by='G', ascending=False)) # 按照一列值排序 # A B C D E F G # 1 1 2019-09-30 2.0 2 b beer 5 # 2 1 2019-09-30 2.0 3 c beer 4 # 3 1 2019-09-30 2.0 4 d beer 4 # 0 1 2019-09-30 2.0 1 a beer 1 print('G,D列排序'.center(50, '-')) print(df_1.sort_values(by=['G', 'D'], ascending=False)) # 按照多列值排序 # A B C D E F G # 1 1 2019-09-30 2.0 2 b beer 5 # 3 1 2019-09-30 2.0 4 d beer 4 # 2 1 2019-09-30 2.0 3 c beer 4 # 0 1 2019-09-30 2.0 1 a beer 1 print('數據按照行排序'.center(50, '-')) index = list(range(4)) col = ['A', 'B', "C", 'D', 'E'] d_sort = pd.DataFrame(np.arange(20).reshape(4, 5), index=index, columns=col) # print(d_sort) print(d_sort.sort_values(by=[1, 2], axis=1, ascending=False)) # 按照行值排序 # E D C B A # 0 4 3 2 1 0 # 1 9 8 7 6 5 # 2 14 13 12 11 10 # 3 19 18 17 16 15
index = ['A', 'B', "C", 'D', 'E'] dates = pd.date_range('20191001', periods=10) df = pd.DataFrame(np.random.randn(10, 5), index=dates, columns=index) # print(df) print('選擇某列'.center(50, '-')) print(df['A']) # 2019-10-01 -0.595401 # 2019-10-02 1.264714 # 2019-10-03 1.179423 # 2019-10-04 -0.516471 # 2019-10-05 0.891850 # 2019-10-06 -0.011205 # 2019-10-07 -0.206089 # 2019-10-08 0.972745 # 2019-10-09 -0.135309 # 2019-10-10 1.590818 # Freq: D, Name: A, dtype: float64 print('切片選擇'.center(50, '-')) print(df[0:3]) # 按照行數切片 print(df['2019-10-02':'2019-10-05']) # 按照索引值進行切片 # A B C D E # 2019-10-01 -0.595401 0.337930 0.034220 1.472752 -0.555414 # 2019-10-02 1.264714 0.518856 -1.148349 1.674159 -0.473919 # 2019-10-03 1.179423 2.036095 -0.719042 1.607909 2.659472 # A B C D E # 2019-10-02 1.264714 0.518856 -1.148349 1.674159 -0.473919 # 2019-10-03 1.179423 2.036095 -0.719042 1.607909 2.659472 # 2019-10-04 -0.516471 1.733509 -0.177231 0.260795 -0.106666 # 2019-10-05 0.891850 0.665301 0.013627 -1.346193 0.222099
# 按照行切片[0:3]值切片到了0-2行
# 按照值'2019-10-02':'2019-10-05'切片,切到完整的日期範圍
print('按照行精確選擇'.center(50, '-')) print(df.loc['2019-10-02', ['A', 'B']]) # 按照行精確選擇列 # A 1.264714 # B 0.518856 # Name: 2019-10-02 00:00:00, dtype: float64 print('行號選擇數據'.center(50, '-')) print(df.iloc[3, 1]) # 1.7335085248615345
# 行數從0開始計數
print(df.iloc[3:5, 0:2]) # 輸出4到到5行的數據,1到2列的數據 # A B # 2019-10-04 -0.516471 1.733509 # 2019-10-05 0.891850 0.665301 # 切片從0開始計數,顧頭部顧尾 print('混合選擇'.center(50, '-')) print(df.ix[0:3, ['B', 'C']]) # B C # 2019-10-01 0.337930 0.034220 # 2019-10-02 0.518856 -1.148349 # 2019-10-03 2.036095 -0.719042 print('條件選擇'.center(50, '-')) print(df[df.A > 0]) # A B C D E # 2019-10-01 0.391314 0.647378 0.065032 -0.436882 -0.482698 # 2019-10-02 1.742555 0.374014 0.737914 1.708461 0.328336 # 2019-10-03 0.024506 -0.455824 -0.397145 1.523103 1.361226 # 2019-10-04 0.140041 -0.604164 -0.397656 -0.423711 -0.626598 # 2019-10-05 0.027898 0.159293 -1.000558 0.921370 -1.613052 # 2019-10-08 1.411249 -1.292006 0.140944 0.699647 -0.065080 # 2019-10-10 0.306495 0.590515 -0.524972 0.521179 -0.805736