数据分箱技术
import numpy as np
import pandas as pd
from pandas import Series,DataFrame
score_list=np.random.randint(25,100,size=20)
print(score_list)
bins=[0,59,70,80,90]
score_cat=pd.cut(score_list ,bins)
print(pd.value_counts(score_cat))
df=DataFrame()
df['score']=score_list
df['Categories']=pd.cut(df['score'],bins,
labels=['low','ok','good','great'])
print(df)
数据分组技术
import numpy as np
import pandas as pd
from pandas import Series,DataFrame
#将数据文件同代码文件放在同一个文件夹,
#读取时在文件名后加.csv后缀
df=pd.read_csv("city_weather.csv.csv")
print(df)
g=df.groupby(df['city'])
print(g)
print(g.groups)
df_bj=g.get_group('BJ')
print(df_bj)
print(df_bj.mean())
print(g.mean())
print(list(g))
print(dict(list(g)))
for name,group in g:
print(name)
print(group_df)
数据聚合技术
import numpy as np
import pandas as pd
from pandas import Series,DataFrame
df=pd.read_csv("city_weather.csv.csv")
g=df.groupby('city')
print(g.describe())
#定义参数
def foo(attr):
print(type(attr)),print(attr)
return np.nan
print(g.agg(foo))
print(g.get_group('BJ'))
for (name1,name2),group in g:
print(name1,name2)
print(group)