in View.py ``` list_2 = [ {'header_c': c_foo1, 'header_d': d_foo1}, {'header_c': c_foo2, 'header_d': d_foo2}, ] ``` in web.html ```
{{ h }} | {% endfor %} {% endif%} {% endfor %}
---|
{{ value}} | {% endfor %}
in View.py ``` list_2 = [ {'header_c': c_foo1, 'header_d': d_foo1}, {'header_c': c_foo2, 'header_d': d_foo2}, ] ``` in web.html ```
{{ h }} | {% endfor %} {% endif%} {% endfor %}
---|
{{ value}} | {% endfor %}
'''
System version : Windows 10 64-bit
Python version : Python 3.6.0 :: Anaconda 4.3.1 (64-bit)
'''
id_list = ['a','b']
sql_id_list = str(tuple([key for key in id_list])).replace(',)', ')')
query ="""
SELECT id FROM table WHERE article_id IN {sql_id_list}
""".format(sql_id_list=sql_id_list)
print("sql_id_list:",sql_id_list)
print("query:",query)
id_list = ['a']
sql_id_list = str(tuple([key for key in id_list])).replace(',)', ')')
query ="""
SELECT id FROM table WHERE article_id IN {sql_id_list}
""".format(sql_id_list=sql_id_list)
print("sql_id_list:",sql_id_list)
print("query:",query)
# cursor.execute(execute_str,values)
sql_id_list: ('a', 'b')
query:
SELECT id FROM table WHERE article_id IN ('a', 'b')
sql_id_list: ('a')
query:
SELECT id FROM table WHERE article_id IN ('a')
'''
System version : Windows 10 64-bit
Python version : Python 3.6.0 :: Anaconda 4.3.1 (64-bit)
'''
import os
print('os.getcwd():', os.getcwd())
print('__file__:', __file__)
print('os.path.basename(__file__):', os.path.basename(__file__))
print('os.path.dirname(__file__):', os.path.dirname(__file__))
print('-'*45)
# 存原始的工作目錄
save_raw_path = os.getcwd()
print("存原始的工作目錄")
print("os.getcwd():",os.getcwd())
print('-'*45)
# 變更工作目錄
go_path = r"D:\tmp"
os.chdir(go_path)
print("變更工作目錄")
print("os.getcwd():",os.getcwd())
print('-'*45)
'''execute your code here'''
# 改回原始的工作目錄
os.chdir(save_raw_path)
print("改回原始的工作目錄")
print("os.getcwd():",os.getcwd())
os.getcwd(): D:\project\實用程式庫\log紀錄
__file__: d:/project/實用程式庫/log紀錄/tmp.py
os.path.basename(__file__): tmp.py
os.path.dirname(__file__): d:/project/實用程式庫/log紀錄
---------------------------------------------
存原始的工作目錄
os.getcwd(): D:\project\實用程式庫\log紀錄
---------------------------------------------
變更工作目錄
os.getcwd(): D:\tmp
---------------------------------------------
改回原始的工作目錄
os.getcwd(): D:\project\實用程式庫\log紀錄
'''
System version : Windows 10 64-bit
Python version : Python 3.6.0 :: Anaconda 4.3.1 (64-bit)
'''
import traceback
try:
print(a)
except Exception as e:
print('simple message:')
print('repr(e):\n',repr(e))
print('-'*45)
print('detail message:')
print('traceback.format_exc():\n',traceback.format_exc())
import pandas as pd
import numpy as np
df1 = pd.DataFrame()
number = [1,2,3,4,5]
sex = ['male','male','female','female','female']
df1['number'] = number
df1['sex'] = sex
df1['income'] = [500,1500,1300,2500,500]
print(df1)
print('-'*45)
def income_group(df):
if df['income']>2000:
return 'high'
elif df['income']>1000:
return 'medium'
else:
return 'low'
df1['group'] = df1.apply(income_group, axis=1)
print(df1)
print('-'*45)
number sex income
0 1 male 500
1 2 male 1500
2 3 female 1300
3 4 female 2500
4 5 female 500
---------------------------------------------
number sex income group
0 1 male 500 low
1 2 male 1500 medium
2 3 female 1300 medium
3 4 female 2500 high
4 5 female 500 low
---------------------------------------------
import pandas as pd
import numpy as np
df1 = pd.DataFrame()
number = [1,2,3,4,5]
sex = ['male','male','female','female','female']
df1['number'] = number
df1['sex'] = sex
df1['income'] = [500,2000,500,2000,500]
print(df1)
print('='*45)
conditions = [
(df1['sex']=='male') & (df1['income']>1000),
(df1['sex']=='male') & (df1['income']<1000),
(df1['sex']=='female') & (df1['income']>1000)
]
choices = ['male-high_income','male-low_income', 'female-high_income']
df1['group'] = np.select(conditions, choices, default='no_group')
print(df1)
number sex income
0 1 male 500
1 2 male 2000
2 3 female 500
3 4 female 2000
4 5 female 500
=============================================
number sex income group
0 1 male 500 male-low_income
1 2 male 2000 male-high_income
2 3 female 500 no_group
3 4 female 2000 female-high_income
4 5 female 500 no_group
import pandas as pd
df1 = pd.DataFrame()
number = [1,2,3,4,5]
sex = ['male','female','female','female','male']
df1['number'] = number
df1['sex'] = sex
df1['b'] = [1,2,3,4,5]
df2 = pd.DataFrame()
number = [1,2,3,4,5]
sex = ['male','female','female','female','male']
df2['number'] = number
df2['sex'] = sex
df2['a'] = [1,2,3,4,5]
print('df1')
print(df1)
print('-'*45)
print('df2')
print(df2)
print('-'*45)
print('未處理欄位名稱,欄位名稱重新排序')
df3 = df1.append(df2,ignore_index =True)
print(df3)
print('-'*45)
print('處理後,欄位名稱會以df1為準,新的欄位依序加入')
columns_all = list(df1.columns)
columns_new = list(df2.columns)
for ele in columns_new:
if ele not in columns_all:
columns_all.append(ele)
df3 = df1.append(df2,ignore_index =True)
df3 = df3[columns_all]
print(df3)
print('-'*45)
df1
number sex b
0 1 male 1
1 2 female 2
2 3 female 3
3 4 female 4
4 5 male 5
---------------------------------------------
df2
number sex a
0 1 male 1
1 2 female 2
2 3 female 3
3 4 female 4
4 5 male 5
---------------------------------------------
未處理欄位名稱,欄位名稱重新排序
a b number sex
0 NaN 1.0 1 male
1 NaN 2.0 2 female
2 NaN 3.0 3 female
3 NaN 4.0 4 female
4 NaN 5.0 5 male
5 1.0 NaN 1 male
6 2.0 NaN 2 female
7 3.0 NaN 3 female
8 4.0 NaN 4 female
9 5.0 NaN 5 male
---------------------------------------------
處理後,欄位名稱會以df1為準,新的欄位依序加入
number sex b a
0 1 male 1.0 NaN
1 2 female 2.0 NaN
2 3 female 3.0 NaN
3 4 female 4.0 NaN
4 5 male 5.0 NaN
5 1 male NaN 1.0
6 2 female NaN 2.0
7 3 female NaN 3.0
8 4 female NaN 4.0
9 5 male NaN 5.0
---------------------------------------------
def unique(a):
return list(set(a))
def intersect(a, b):
return list(set(a) & set(b))
def union(a, b):
return list(set(a) | set(b))
a = [1,2,3,4,4]
b = [2,3,5]
print( unique(a) )
print( intersect(a, b) )
print( union(a, b) )
[1, 2, 3, 4]
[2, 3]
[1, 2, 3, 4, 5]
number = [1,2,3,4,5]
sex = ['male','female','female','female','male']
df_new = pd.DataFrame()
df_new['number'] = number
df_new['sex'] = sex
print(df_new)
number sex
0 1 male
1 2 female
2 3 female
3 4 female
4 5 male
df_new.drop_duplicates(['sex'])
number sex
0 1 male
1 2 female
import ast
str1 = "{'a' : 1, 'b' : 2}"
dict1 = ast.literal_eval(str1)
print(dict1)
{'a': 1, 'b': 2}
A = set([1,2,3,4])
B = set([2,3,5])
# 交集
set_list = [A,B]
intersection_A_B = set.intersection(*set_list)
# 差集
difference_A_B = A.difference(B)
difference_B_A = B.difference(A)
# 聯集
union_A_B = A.union(B)
print('交集')
print('intersection_A_B :',intersection_A_B)
print('差集')
print('difference_A_B :',difference_A_B)
print('difference_B_A :',difference_B_A)
print('聯集')
print('union_A_B :',union_A_B)
交集
intersection_A_B : {2, 3}
差集
difference_A_B : {1, 4}
difference_B_A : {5}
聯集
union_A_B : {1, 2, 3, 4, 5}
number = [1,2,3,4,5]
sex = ['male','female','female','female','male']
df_new = pd.DataFrame()
df_new['number'] = number
df_new['sex'] = sex
print(df_new)
number sex
0 1 male
1 2 female
2 3 female
3 4 female
4 5 male
from pandasql import sqldf
q = 'SELECT * FROM df_new where sex = "male";'
df_one= sqldf(q)
print(df_one)
number sex
0 1 male
1 5 male