Python 筆記 #13# Pandas: Viewing Data
阿新 • • 發佈:2018-01-18
.html count otl blank scribe .py and 簡單的 als
感覺很詳細:數據分析:pandas 基礎
import pandas as pd import numpy as np import matplotlib.pyplot as plt dates = pd.date_range(‘20180116‘, periods=3) # 創建 16 17 18 等六個日期 df = pd.DataFrame(np.random.randn(3,4), index=dates, columns=list(‘ABCD‘)) # 這是二維的,類似於一個表! # 通過 numpy 隨機了一個 3 * 4 的數據,這和行數、列數是相對應的# print(df) # A B C D # 2018-01-16 -0.139759 0.857653 0.754470 0.224313 # 2018-01-17 1.565070 0.521973 -1.265168 -0.278524 # 2018-01-18 -0.668574 -0.527155 0.877785 -1.123334 # print(df.head(1)) # 默認值是 5 # A B C D #2018-01-16 -0.039203 1.211976 0.664805 0.307147 df.tail(5) # 同上,顧名思義 # print(df.index) # 顧名思義 + 1 # print(df.columns) # DatetimeIndex([‘2018-01-16‘, ‘2018-01-17‘, ‘2018-01-18‘], dtype=‘datetime64[ns]‘, freq=‘D‘) # Index([‘A‘, ‘B‘, ‘C‘, ‘D‘], dtype=‘object‘) # print(df.describe()) # 對每列數據做一些簡單的統計學處理# A B C D # count 3.000000 3.000000 3.000000 3.000000 # mean -0.163883 -0.107242 -0.621706 0.618341 # std 0.360742 0.429078 0.800366 0.609524 # min -0.505212 -0.502887 -1.352274 0.055032 # 25% -0.352602 -0.335291 -1.049444 0.294803 # 50% -0.199991 -0.167695 -0.746613 0.534574 # 75% 0.006782 0.090581 -0.256421 0.899995 # max 0.213556 0.348857 0.233770 1.265416 # print(df.T) # 轉置(Transposing) # 2018-01-16 2018-01-17 2018-01-18 # A -1.137015 -0.067200 0.737709 # B -1.141811 0.335953 1.023016 # C 2.481266 -0.957599 0.011144 # D 1.485434 -0.605588 0.592746 # print(df) # print(df.sort_index(axis=1, ascending=False)) # axis=1 按照列名排序 axis=0 按照行名排序 # A B C D # 2018-01-16 -0.787226 0.321619 1.097938 -0.701082 # 2018-01-17 -0.417257 -0.163390 -0.943166 -0.497475 # 2018-01-18 0.486670 -0.733582 1.923475 -1.145891 # D C B A # 2018-01-16 -0.701082 1.097938 0.321619 -0.787226 # 2018-01-17 -0.497475 -0.943166 -0.163390 -0.417257 # 2018-01-18 -1.145891 1.923475 -0.733582 0.486670 # print(df.sort_values(by=‘B‘)) # A B C D # 2018-01-17 0.817088 -0.792903 1.643429 -0.008784 # 2018-01-18 0.540910 0.662119 0.190846 -0.960926 # 2018-01-16 0.333727 1.196133 -0.527796 0.677337
Python 筆記 #13# Pandas: Viewing Data