PandasのDataFrameをgroupごとにプロットする。
前提
%matplotlib inline
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
import matplotlib
matplotlib.style.use('ggplot')#これでggplotのような画像ができる。
今回使うデータ(別にもっと簡単なのでいい。)
%matplotlib inline
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
import matplotlib
matplotlib.style.use('ggplot')#これでggplotのような画像ができる。
entropyを計算し終わったDataFrame loc_entropyは以下の通り
loc_entropy.head()
mean | std | count | entropy | prob | ||
---|---|---|---|---|---|---|
user_id | hour | |||||
0 | 0203884 | 3078.776380 | 131 | 2.275733 | 0.056466 | |
1 | 203783 | 2640.164957 | 100 | 2.380120 | 0.043103 | |
2 | 205211 | 2045.731898 | 101 | 1.210215 | 0.043534 | |
3 | 205454 | 1808.111722 | 91 | 0.864845 | 0.039224 | |
4 | 202456 | 3101.095935 | 41 | 2.916620 | 0.017672 |
user_idごとに、entropyのcolumnを折れ線グラフにする。(横軸 hour, 縦軸 entropy)
for key, grp in loc_entropy.head(500).reset_index().groupby('user_id'):
plt.plot(grp['entropy'],label=key)
plt.show()
for key, grp in loc_entropy.head(500).reset_index().groupby('user_id'):
plt.plot(grp['entropy'],label=key)
plt.show()
ついでにuser_idごとに、entropyをhistで見る(横軸 entropy, 縦軸 頻度)
for key, grp in time_entropy.head(1000).reset_index().groupby('user_id'):
if len(grp['entropy']) != 1:
plt.hist(np.array(grp['entropy']))
plt.show()
for key, grp in time_entropy.head(1000).reset_index().groupby('user_id'):
if len(grp['entropy']) != 1:
plt.hist(np.array(grp['entropy']))
plt.show()
今回は、time_entropyという別のDFを用いたので、結果も少し異なる。
もう一つは直接DF.plot(data,by='group')を使う
time_entropy.head(2000).reset_index().hist('entropy', by='user_id')
これでやると、以下のように、一つのfigureの中に何個もグラフができるので、user_idが多いときは注意が必要。
Author And Source
この問題について(PandasのDataFrameをgroupごとにプロットする。), 我々は、より多くの情報をここで見つけました https://qiita.com/nakamasato/items/0c651566944c64e5efcb著者帰属:元の著者の情報は、元のURLに含まれています。著作権は原作者に属する。
Content is automatically searched and collected through network algorithms . If there is a violation . Please contact us . We will adjust (correct author information ,or delete content ) as soon as possible .