Pandas-office-10分钟开始 发表于 2017-02-15 | 分类于 Python | | 基本1234# -*- coding:utf-8 -*-import numpy as npimport pandas as pdimport matplotlib.pyplot as plt 1、创建数据框1234dates = pd.date_range('20130101', periods=6)df = pd.DataFrame(np.random.randn(6, 4), index=dates, columns=list('ABCD'))left = pd.DataFrame({'key': ['foo', 'foo'], 'lval': [1, 2]})right = pd.DataFrame({'key': ['foo', 'foo'], 'rval': [4, 5]}) 2、查看数据123456df.head()df.tail(3)df.describe()df.Tdf.sort_index(axis=1, ascending=False) # 按照行名、列名排列df.sort_values(by='B') 3、选择12# .loc()# .iloc() 4、缺失值1234df['E'] = [1, 2, np.nan, 1, 2, np.nan]df.dropna(how='any')df.fillna(value=5)pd.isnull(df) 5、运用1df.apply(np.cumsum) 6、交并123456df = pd.DataFrame(np.random.randn(10, 4))pieces = [df[:3], df[3:7], df[7:]]pd.concat(pieces, axis=0)pd.concat([df, df], axis=1)pd.merge(left, right, on='key')left.merge(right, on='key') 7、groupby123456df = 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)})df.groupby('A').sum()df.groupby(['A', 'B']).sum() 8、重组数据框123456789tuples = list(zip(*[['bar', 'bar', 'baz', 'baz', 'foo', 'foo', 'qux', 'qux'], ['one', 'two', 'one', 'two', 'one', 'two', 'one', 'two']]))index = pd.MultiIndex.from_tuples(tuples, names=['first', 'second'])df = pd.DataFrame(np.random.randn(8, 2), index=index, columns=['A', 'B'])df2 = df[:4]stacked = df2.stack()stacked.unstack()stacked.unstack(1)stacked.unstack(0) Pivot Tables9、时间序列123456789rng = pd.date_range('1/1/2012', periods=100, freq='S') # 按秒进行rng2 = pd.date_range('3/6/2012 00:00', periods=5, freq='D') # 按天进行rng3 = pd.date_range('1/1/2012', periods=5, freq='M') # 按月进行,保留天数ts = pd.Series(np.random.randn(len(rng3)), index=rng3)ps = ts.to_period() # 天变为月,仅保留月数ps.to_timestamp() # 月变为天prng = pd.period_range('1990Q1', '2000Q4', freq='Q-NOV') # 按季度进行ts = pd.Series(np.random.randn(len(prng)), prng)ts.index = (prng.asfreq('M', 'e') + 1).asfreq('H', 's') + 9 # 季度转化为日期,指定时间 10、Categoricals 分类的使用12345df = pd.DataFrame({"id": [1, 2, 3, 4, 5, 6], "raw_grade": ['a', 'b', 'b', 'a', 'a', 'e']})df["grade"] = df["raw_grade"].astype("category")df["grade"].cat.categories = ["very good", "good", "very bad"]df["grade"] = df["grade"].cat.set_categories(["very bad", "bad", "medium", "good", "very good"])df.sort_values(by="grade")df.groupby("grade").size() 11、画图12345ts = pd.Series(np.random.randn(1000), index=pd.date_range('1/1/2000', periods=1000))ts = ts.cumsum()ts.plot()df = pd.DataFrame(np.random.randn(1000, 4), index=ts.index, columns=['A', 'B', 'C', 'D'])df = df.cumsum()df.plot(); plt.legend(loc='best') 坚持原创技术分享,您的支持将鼓励我继续创作! 赏 微信打赏 支付宝打赏