将某一列的内容变成 columns 另外一列的内容变成 index, 第三列的内容作为 values的方法.
-
以’ act’作为 index, pandas 针对 index 提供检索的功能 .loc[]
-
eg: ((pd.DataFrame(d4).round(1)).set_index(‘act’)).loc[‘sleep] 即在所有 index 中检出行为 在这个基础上, 再使用 .set_index(‘week’), 将周作为 index
-
事先建立一个纵轴为52周的矩阵, 在这个矩阵后面添加过滤过的矩阵.
1 2 3 4 5
|
ls = list(range(52)) df3=pd.DataFrame(ls) for act in UniqueAct: df3[act]= (to.loc[act]).set_index('week') df3.tail(1)
|
Indexing and Selecting Data
举例, 睡眠包括午睡和晚上的睡眠
1 2
| sl =df3.loc[:,['12.sleep','12.sleep:noonsleep']] sl[44:45]
|
下面分别为 live, healthfun, input,output 的分类表格
1 2 3 4 5
| fa = df3.loc[:,['08.familylife:washingbeauty','08.familylife:generalAffair','08.familylife:dinner','08.familylife:families','08.familylife:finance']] hf = df3.loc[:,['09.HealthFun:sport', '09.HealthFun:fun','09.HealthFun', '09.HealthFun:fantasy']] ip = df3.loc[:,['04.StudyGrow:reading','07.social', '07.social:networking']] op = df3.loc[:,['04.StudyGrow:ComputerThinking','04.StudyGrow:writer','05.Career', '04.StudyGrow:law', '04.StudyGrow']]
|
计算以上各类的总用时, 添加到 df3后面
将各大类的总用时再列一张表, 就是5大类总用时的表格
1 2 3 4 5 6 7 8 9
| df3['Sleep'] = df3.apply(lambda x: x['12.sleep:noonsleep']+x['12.sleep'], axis=1) df3['Live'] = df3.apply(lambda x: x['08.familylife:washingbeauty']+x['08.familylife:families']+x['08.familylife:dinner']+x['08.familylife:generalAffair']+x['08.familylife:finance'], axis=1) df3['HealthFun'] = df3.apply(lambda x: x['09.HealthFun:sport']+x['09.HealthFun']+x['09.HealthFun:fantasy']+x['09.HealthFun:fun'], axis=1) df3['Input'] = df3.apply(lambda x: x['04.StudyGrow:reading']+x['07.social']+x['07.social:networking'], axis=1) df3['Output'] = df3.apply(lambda x: x['04.StudyGrow:ComputerThinking']+x['04.StudyGrow:writer']+x['05.Career']+x['04.StudyGrow:law']+x[ '04.StudyGrow'], axis=1) ti =df3.loc[:,['Output','Input','Sleep','Live','HealthFun']] ti[44:45]
|
Matplotlib 是经典的 jupyter notebook 可视化框架, 这里快速展示一下, 其他可视化框架的原理都跟这差不多.
1 2 3 4
| import matplotlib.pyplot as plt ti.plot.bar(stacked=True); plt.show()
|
1 2 3 4
| pt = figure(title='5 types') pt.vbar_stack(ti.columns, x = ti.index,width = 0.9,color=Spectral5,source = source3, legend=[value(x) for x in ti.columns]) pt.legend.location = "top_left"
|
Quickstart — Bokeh有介绍.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
| from bokeh.plotting import figure from bokeh.palettes import GnBu5,Greens3,Spectral5,Oranges5,Reds4 from bokeh.core.properties import value HEIGHT = 300 source3 = ColumnDataSource(ti) pt = figure(title='5 types') pt.vbar_stack(ti.columns, x = ti.index,width = 0.9,color=Spectral5,source = source3, legend=[value(x) for x in ti.columns]) pt.legend.location = "top_left" po = figure(height=HEIGHT) source1 = ColumnDataSource(op) po.vbar_stack(op.columns, x = op.index,width = 0.9,color=GnBu5,source = source1, legend=[value(x) for x in op.columns]) po.legend.location = "top_left" source2 = ColumnDataSource(ip) pi = figure(height=HEIGHT,title='Input') pi.vbar_stack(ip.columns, x = ip.index,width = 0.9,color=Greens3,source = source2, legend=[value(x) for x in ip.columns]) pi.legend.location = "top_left" colors = ['#ffffcc','#FDE724' ] ps = figure(height=HEIGHT) source4 = ColumnDataSource(sl) ps = figure(height=HEIGHT,title='sleep') ps.vbar_stack(sl.columns, x = sl.index,width = 0.9,color=colors,source = source4, legend=[value(x) for x in sl.columns]) ps.legend.location = "top_left" source5= ColumnDataSource(fa) pf = figure(height=HEIGHT,title='live') pf.vbar_stack(fa.columns, x = fa.index,width = 0.9,color=Oranges5,source = source5, legend=[value(x) for x in fa.columns]) pf.legend.location = "top_left" source6= ColumnDataSource(hf) ph = figure(height=HEIGHT,title='healthfun') ph.vbar_stack(hf.columns, x = hf.index,width = 0.9,color=Reds4,source = source6, legend=[value(x) for x in hf.columns]) ph.legend.location = "top_left"
|
- Div是可以识别 html 排版语言的小窗口, 把你想要显示的文字填在
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| ```python from bokeh.layouts import widgetbox from bokeh.models.widgets import Div t0 = Div(text=""" <h1 align="center">Time Report</h1> <p>Each Nature week has 168 hours(24h*7d=168h).</p> <p>Hours in this form are a little more or less. One reason is some time may not be recorded,the other reason is an event time may cross two weeks.</p> <p>Each bar in the plot nearly touchs 168(y), this shows I record all time-spent every week. </p> <h2> 5 big types</h2> <p>Label daily action to 5 big types, like the plot shows. </p> """,width=WIDTH, height=200)
|
除了文字, 还可以填写 function, 给 variable 赋值, 即可显示对应的输出结果.
- frequency 是我编写的 function, 会在下文详解.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| from lgh7SumFrequency import frequency t1 = Div(text='''<h2>Output ↑</h2>''' +frequency(cvsf,'04.StudyGrow:ComputerThinking')+'''<p>\n</p>''' +frequency(cvsf,'04.StudyGrow:writer')+'''<p>\n04.StudyGrow shows curious.</p>''',width=WIDTH, height=100) t2 = Div(text='''<h2>Input ↑</h2> <p>Reading without output is input.</p> <p>Meaningly social is in this part. Others belong to live:familes.\n<p>''' +frequency(cvsf,'04.StudyGrow:reading'),width=WIDTH, height=100) t3 = Div(text='''<h2>Sleep ↓</h2> <p>sleep < 59h</p>''',width=WIDTH, height=100) t4 = Div(text='''<h2>Live</h2><p>08.familylife:familes includes other social.</p>''' +frequency(cvsf,'08.familylife:washingbeauty'),width=WIDTH, height=100) t5 = Div(text='''<h2>HealthFun</h2>'''+frequency(cvsf,'09.HealthFun:sport')+'''\n<p>09.Healthfun shows curious.</p> <p>09.Healthfun:fun should be down.</p> ''',width=WIDTH, height=100,)
|
- 排版, 可以按照坐标的方式, 即 grid, 也可以按照 row 和 column 排列, 这里选择 grid , [widgetbox(t0),none] 则会将他们排在一行, 其他的一次排下去. Laying out Plots and Widgets
1 2 3 4 5 6
| from bokeh.layouts import gridplot grid = gridplot([[widgetbox(t0)],[pt],[t1], [po],[t2],[pi],[t3],[ps],[t4],[pf],[t5],[ph]]) save(grid)
|
即可生成 HTML, 输出成果见time report
- Learn Python the Hard Way 抄了一个月的代码.
- 看 pandas 来写代码了. 大妈提供了 Python Data Science Handbook | Python Data Science Handbook 这本数据科学工具书. 我并没有全看完, 只是把每个工具都有的10分钟快速掌握看完, 知道它主要实现什么功能, 然后就去完成自己的需求. 千万不要抱着都看完才去学, 这样无法开动, 带着项目搜索怎么做, 并不断的实验, 是最快的.
- 经常会遇到报错, 见这篇: When Your Computer Answer ‘Error’ 写代码遇到报错怎么办? | Li Guanghe’s blog
- 这中间遇到的最大的坑, 是 type. 这篇教程一定要仔细看. Understanding Data Types in Python | Python Data Science Handbook
- 整个尝试过程在 Ipython 中完成, 然后导出成为脚本. 设置成 function 供调用.
- 尝试过程的 Ipython 文档本身导出到 .md 格式, 就是本教程.