pythonプロジェクト適用例(四)pandasでhtmlテーブルに結果を書き込む
参照先:https://blog.csdn.net/tz_zs/article/details/81137998
公式APIドキュメント:http://pandas.pydata.org/pandas-docs/version/0.19.0/generated/pandas.DataFrame.to_html.html
目次
一、表の作成
1.1ルーチン
二、結果を書く
2.1 index
2.2 df
2.3 htmlファイルへの書き込み
2.4結果
一、表の作成
https://blog.csdn.net/tz_zs/article/details/81137998
1.1ルーチン
二、結果を書く
2.1 index
左の縦の列で、dfのkeyとは違います.dfの要素はindexと1つ1つ対応しています
2.2 df
その中の要素の個数はindexの要素の個数と同じでなければならない.
例えばこのような書き方では新聞の間違いが発生します
リストを作成してから書きます
2.3 htmlファイルへの書き込み
headとbodyを直接作成しhtmlファイルに書き込む
2.4結果
書き込み成功
三、ルーチン
王の栄光の護甲値と実際のダメージの割合の関係を演算します
公式APIドキュメント:http://pandas.pydata.org/pandas-docs/version/0.19.0/generated/pandas.DataFrame.to_html.html
目次
一、表の作成
1.1ルーチン
二、結果を書く
2.1 index
2.2 df
2.3 htmlファイルへの書き込み
2.4結果
一、表の作成
https://blog.csdn.net/tz_zs/article/details/81137998
1.1ルーチン
# -*- coding: utf-8 -*-
"""
@author: tz_zs
"""
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
index = ["2018-07-01", "2018-07-02", "2018-07-03", "2018-07-04"]
df = pd.DataFrame(index=index)
df[" "] = [11, 12, 13, 14]
df[" "] = [21, 22, 23, 24]
print(df)
"""
2018-07-01 11 21
2018-07-02 12 22
2018-07-03 13 23
2018-07-04 14 24
"""
axes_subplot = df.plot()
# print(type(axes_subplot)) #
plt.xlabel("time")
plt.ylabel("num")
plt.legend(loc="best")
plt.grid(True)
plt.savefig("test.png")
HEADER = '''
'''
FOOTER = '''
''' % ("test.png")
with open("test.html", 'w') as f:
f.write(HEADER)
f.write(df.to_html(classes='df'))
f.write(FOOTER)
二、結果を書く
2.1 index
左の縦の列で、dfのkeyとは違います.dfの要素はindexと1つ1つ対応しています
def write_clases_result_into_html(self):
print('writing result into html')
index=[]
for class_idx in range(self.class_num):
index.append('class'+str(class_idx))
df=pd.DataFrame(index=index)
2.2 df
その中の要素の個数はindexの要素の個数と同じでなければならない.
例えばこのような書き方では新聞の間違いが発生します
df['weight'],df['precision'],df['recall'],df['F1']=[],[],[],[]
'''
File "badcase_analyse.py", line 180, in write_clases_result_into_html
df['weight'],df['precision'],df['recall'],df['F1']=[],[],[],[]
File "/home/xingxiangrui/chun-ML_GCN/env/lib/python3.6/site-packages/pandas/core/frame.py", line 3370, in __setitem__
self._set_item(key, value)
File "/home/xingxiangrui/chun-ML_GCN/env/lib/python3.6/site-packages/pandas/core/frame.py", line 3445, in _set_item
value = self._sanitize_column(key, value)
File "/home/xingxiangrui/chun-ML_GCN/env/lib/python3.6/site-packages/pandas/core/frame.py", line 3630, in _sanitize_column
value = sanitize_index(value, self.index, copy=False)
File "/home/xingxiangrui/chun-ML_GCN/env/lib/python3.6/site-packages/pandas/core/internals/construction.py", line 519, in sanitize_index
raise ValueError('Length of values does not match length of index')
ValueError: Length of values does not match length of index
'''
リストを作成してから書きます
weight_list,precision_list,recall_list,F1_list=[],[],[],[]
# df['weight'],df['precision'],df['recall'],df['F1']=[],[],[],[]
self.cls_P, self.cls_R, self.cls_F1,self.cls_weight
for class_idx in range(self.class_num):
weight_list.append(self.cls_weight[class_idx])
precision_list.append(self.cls_P[class_idx])
recall_list.append(self.cls_R[class_idx])
F1_list.append(self.cls_F1[class_idx])
df['weight']=weight_list
df['precision']=precision_list
df['recall']=recall_list
df['F1']=F1_list
2.3 htmlファイルへの書き込み
headとbodyを直接作成しhtmlファイルに書き込む
df['weight']=weight_list
df['precision']=precision_list
df['recall']=recall_list
df['F1']=F1_list
HEADER = '''
'''
FOOTER = '''
'''
with open(self.html_path, 'w') as f:
f.write(HEADER)
f.write(df.to_html(classes='df'))
f.write(FOOTER)
2.4結果
書き込み成功
三、ルーチン
王の栄光の護甲値と実際のダメージの割合の関係を演算します
#!/usr/bin/python
# -*- coding: UTF-8 -*-
"""
"""
import numpy as np
import matplotlib.pyplot as plt
import math
import pandas as pd
################ 1.
plt.figure(1)
#
AR=[]
AD_ratio=[]
AR_all=np.linspace(0,1500,10) # 0-1500
for AR_item in AR_all:
AR.append(AR_item)
AD_ratio.append(602/(602+AR_item)*100)
#
plt.plot(AR,AD_ratio,marker='o')
plt.xlim(0,1500)
plt.grid( color = 'blue',linestyle='-',linewidth = 0.3)
plt.ylim(0,100)
for a, b in zip(AR, AD_ratio):
a=round(a,0) #
b=round(b,1)
plt.text(a, b, (a,b),ha='center', va='bottom', fontsize=10)
plt.title(' AD %', fontproperties="SimSun")
plt.xlabel('AR ', fontproperties="SimSun")
plt.ylabel('AD ', fontproperties="SimSun")
#plt.show()
#
AR=[]
AD_ratio=[]
ratio_decrese=[]
AR_all=np.linspace(0,1500,10) # 0-1500 ,
for AR_item in AR_all:
AR.append(round(AR_item,0))
AD_ratio.append(round(602/(602+AR_item)*100,2))
ratio_decrese.append(round((602/(602+AR_item)-602/(602+AR_item+100))*100,2))
df = pd.DataFrame(index=AR)
df["AD_ratio"]=AD_ratio
df["ratio_decrese"]=ratio_decrese
HEADER = '''
'''
FOOTER = '''
'''
with open("C:\\Users\\xingxiangrui\Desktop\\ \\test.html", 'w') as f:
f.write(HEADER)
f.write(df.to_html(classes='df'))
f.write(FOOTER)
print("Program done!")