Pandas-office-10分钟开始

来源:互联网 发布:农业供给侧改革 知乎 编辑:程序博客网 时间:2024/05/22 07:53

基本

# -*- coding:utf-8 -*-import numpy as npimport pandas as pdimport matplotlib.pyplot as plt

1、创建数据框

使用numpy创建

dates = pd.date_range('20130101', periods=6)df = pd.DataFrame(np.random.randn(6, 4), index=dates, columns=list('ABCD'))

使用dict创建

left = pd.DataFrame({'key': ['foo', 'foo'], 'lval': [1, 2]})right = pd.DataFrame({'key': ['foo', 'foo'], 'rval': [4, 5]})

2、查看数据

基本查看

df.head()df.tail(3)df.describe()df.T

pandas排序

df.sort_index(axis=1, ascending=False)  # 按照行名、列名排列df.sort_values(by='B')

3、选择

按照index、column名字索引

# .loc()

按照绝对位置索引

# .iloc()

4、缺失值

df['E'] = [1, 2, np.nan, 1, 2, np.nan]df.dropna(how='any')df.fillna(value=5)pd.isnull(df)

5、运用

df.apply(np.cumsum)

6、交并

df = 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、groupby

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)})df.groupby('A').sum()df.groupby(['A', 'B']).sum()

8、重组数据框

复合索引的列名降低

tuples = 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 Tables

9、时间序列

不同频率下的date_range

rng = 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 分类的使用

分类的产生

df = 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、画图

ts = 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')
0 0
原创粉丝点击