维度表示(take)
values = pd.Series([0,1,0,0]*2)
dim = pd.Series(['apple','orange'])
dim.take(values)
Categorical
fruit_cat = df['fruit'].astype('category')
c.categoricals
c.codes
df['fruit'] = df['fruit'].astype('category')
c = pd.Categorical(['foo','bar'])
c=pd.Categorical.from_codes(codes,categories)
c.as_ordered()
使用Categorical计算
bins = pd.qcut(draws,4)
bins = pd.Series(bins,name='quartiles')
result = (pd.Series(draws).groupby(bins))
分类方法
cat_s.cat.set_categories(list)
cat_s.remove_unused_categories()
Categorical转df(get_dummies)
cat_s = pd.Series(['a','b','c','d']*2,dtype = 'category')
pd.get_dummies(cat_s)
GroupBy
transform
g = df.groupby('key').value
g.mean()
g.transform(lambda x: x.mean())
g.transform('mean')
g.transform(lambda x: s.rank(ascending = False))
分组时间重采样
df.set_index('time').resample('5min').count()
time_key = pd.TimeGrouper('5min')
resampled = df.set_index('time').groupoby(['key',time_key])
方法链
assign赋值
df2 = df.assign(k=v)
pipe方法(调用自定义或第三方函数)
result = df.pipe(f)
.pipe(g)
.pipe(h)