示例代码如下。
from odps import DataFrame
import numpy as np
import pandas as pd
iris = DataFrame(o.get_table('pyodps_iris'))
#判断是否为null。
print iris.sepallength.isnull().head(5)
#逻辑判断。
print (iris.sepallength > 5).ifelse('gt5','lte5').rename('cmp5').head(5)
#多条件判断。
print iris.sepallength.switch(4.9,'eq4.9',5.0,'eq5.0',default='noeq').rename('equalness').head(5)
from odps.df import switch
print switch(iris.sepallength == 4.9,'eq4.9',iris.sepallength == 5.0,'eq5.0',default='noeq').rename('equalness').head(5)
#修该数据集某一列的一部分值。
iris[iris.sepallength > 5,'cmp5'] = 'gt5'
iris[iris.sepallength <=5,'cmp5'] = 'lte5'
print iris.head(5)
#数学计算。
print (iris.sepallength * 10).log().head(5)
fields = [iris.sepallength,(iris.sepallength /2).rename('sepallength/2'),(iris.sepallength ** 2).rename('sepallength的平方')]
print iris[fields].head(5)
print (iris.sepallength < 5).head(5)
#集合类型相关操作。
data = {'id': [1,2], 'a': [['a1','b1'],['c1']], 'b': [{'a2': 0, 'b2': 1, 'c2': 2},{'d2': 3, 'e2': 4}]}
df = pd.DataFrame(data)
print df
df1 = DataFrame(df, unknown_as_string=True, as_type={'a': 'list<string>','b' : 'dict<string,int64>'})
print df1.dtypes
print df1.head()
print df1[df1.id,df1.a[0],df1.b.len()].head()
print df1.a.explode().head()
print df1.a.explode(pos=True).head()
print df1.b.explode().head()
print df1.b.explode(['key','value']).head()
#explode与其他并列多行输出。
print df1[df1.id,df1.a.explode()].head()
#isin,notin,cut。
#isin用于判断Sequence里的元素是否在某个集合元素里;notin反之。
print iris.sepallength.isin([4.9,5.1]).rename('sepallength').head()
#cut提供离散化的操作,可以将Sequence的数据拆成几个区段。
print iris.sepallength.cut(range(6),labels=['0-1','1-2','2-3','3-4','4-5']).rename('sepallength_cut').head(5)
#include_under和include_over可以分别包括向下和向上的区间。
labels = ['0-1', '1-2', '2-3', '3-4', '4-5', '5-']
iris.sepallength.cut(range(6), labels=labels, include_over=True).rename('sepallength_cut').head(5)