计数
代码语言:javascript复制s = pd.Series(np.random.randint(0, 7, size=10))
s.value_counts()
把数据拼接起来
代码语言:javascript复制df = pd.DataFrame(np.random.randn(10, 4))
pieces = [df[:3], df[3:7], df[7:]]
pd.concat(pieces)
Join ( left, right, inner, outer )
http://pandas.pydata.org/pandas-docs/stable/merging.html#merging-join
代码语言:javascript复制left = pd.DataFrame({'key' : ['foo', 'foo'],
'lval' : [1, 2]})
right = pd.DataFrame({'key' : ['foo', 'foo'],
'rval' : [4, 5]})
print left
print right
pd.merge(left, right, on='key')
分组统计 groupby
代码语言:javascript复制df = pd.DataFrame({'A' : ['foo', 'bar', 'foo', 'bar',
'foo', 'bar', 'foo', 'bar'],
'B' : ['one', 'one', 'two', 'three',
'two', 'two', 'one', 'three'],
'C' : np.random.randn(8),
'D' : np.random.randn(8)})
print df
print df.groupby(['A', 'B']).sum()
Pivot table
代码语言:javascript复制df = pd.DataFrame({'A' : ['one', 'one', 'two', 'three'] * 3,
'B' : ['A', 'B', 'C'] * 4,
'C' : ['foo', 'foo', 'foo', 'bar', 'bar', 'bar'] * 2,
'D': np.random.randn(12),
'E' : np.random.randn(12)})
pd.pivot_table(df, values='D', index=['A', 'B'], columns=['C'])
生成时间序列
代码语言:javascript复制# freq='S' 秒的递进
rng = pd.date_range('1/1/2012', periods=100, freq='S')
print rng[:5]
ts = pd.Series(np.random.randint(0, 500, len(rng)), index=rng)
print ts.head()
给数据加类别标签
代码语言:javascript复制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")
print df
df["grade"].cat.categories = ["very good", "good", "very bad"]
df["grade"] = df["grade"].cat.set_categories(["very bad", "bad", "medium ", "good", "very good"])
print df
print df.groupby("grade").size()
画图
代码语言:javascript复制ts = pd.Series(np.random.randn(1000), index=pd.date_range('1/1/2000', periods=1000))
df = pd.DataFrame(np.random.randn(1000, 4), index=ts.index,
columns=['A', 'B', 'C', 'D'])
df = df.cumsum()
plt.figure(); df.plot(); plt.legend(loc='best')
读取写入 csv,excel 文件
代码语言:javascript复制df.to_csv('foo.csv')
pd.read_csv('foo.csv')
df.to_excel('foo.xlsx', sheet_name='Sheet1')
pd.read_excel('foo.xlsx', 'Sheet1', index_col=None, na_values=['NA']