#!/usr/bin/env python # coding: utf-8 # In[12]: import pandas as pd import numpy as np import matplotlib.pyplot as plt s = pd.Series([1,3,5,np.nan,6,8]) s # In[13]: # Cteate a DataFrame dates = pd.date_range('20130101', periods=6) # dates df = pd.DataFrame(np.random.randn(6,4),index = dates,columns=list('ABCD')) df # In[14]: df.dtypes # In[15]: df.head(2) # In[16]: df.tail(2) # In[20]: # Display the index, columns, and the underlying numpy data df.index df.columns df.values # In[17]: # 查看描述性统计 df.describe() # In[19]: s = pd.Series([1,3,5,np.nan,6,8], index=dates).shift(2) s # In[21]: # Return cumulative sum over requested axis. df.apply(np.cumsum) # In[30]: # Concat function piece = [df[:2],df[2:3],df[3:1]] pd.concat(piece) # In[29]: # Merge function left = pd.DataFrame({'key': ['foo', 'foo'], 'lval': [1, 2]}) right = pd.DataFrame({'key': ['foo', 'foo'], 'lval': [4, 5]}) pd.merge(left,right,on='key') # In[38]: # Append function df = pd.DataFrame(np.random.randn(8, 4), columns=['A','B','C','D']) s = df.iloc[3] print(df) print(s) df.append(s, ignore_index=True) # In[39]: df = pd.DataFrame({'A' : ['foo', 'bar', 'foo', 'bar', 'foo', 'bar', 'foo', 'foo'], 'B' : ['one', 'one', 'two', 'three', 'two', 'two', 'one', 'three'], 'C' : np.random.randn(8), 'D' : np.random.randn(8)}) # Grouping and then applying a function sum to the resulting groups. print(df) df.groupby('A').sum() # In[44]: # Time Series rng = pd.date_range('1/1/2012', periods=100, freq='S') # print(rng) ts = pd.Series(np.random.randint(0, 500, len(rng)), index=rng) # print(ts) ts.resample('5Min').sum() # In[45]: # ts = pd.Series(np.random.randn(1000), index=pd.date_range('1/1/2000', periods=1000)) ts=pd.Series(np.random.randn(1000),index=pd.date_range('1/1/2000',periods=1000)) ts = ts.cumsum() ts.plot()