Open roberthsu2003 opened 1 month ago
code
import pandas as pd
def to_str(value):
value = str(value)[::-1]
value2 = value[0]
for i in range(1, len(value)):
if i % 3 == 0:
value2 += ","
value2 += value[i]
return value2[::-1]
df1 = pd.read_csv('上市公司資料.csv')
df2 = df1.iloc[:, 2:7]
df3 = df2.set_index("公司名稱")
df3["公司代號"] = df3["公司代號"].astype(int)
df3["營業收入-當月營收"] = df3["營業收入-當月營收"].map(to_str)
df3["營業收入-上月營收"] = df3["營業收入-上月營收"].map(to_str)
df3 #直接print格式會跑掉
result
df1=pd.read_csv('上市公司資料.csv')
df2 = df1.drop(columns = ['出表日期','資料年月','營業收入-去年當月營收','營業收入-上月比較增減(%)','營業收入-去年同月增減(%)','累計營業收入-當月累計營收','累計營業收入-去年累計營收','累計營業收入-前期比較增減(%)','備註'])
def to_str(value):
thousand_sep = format(value, ",d")
return str(thousand_sep)
df2['公司代號'] = df2['公司代號'].astype(str)
df2['營業收入-當月營收'] = df2['營業收入-當月營收'].map(to_str)
df2['營業收入-上月營收'] = df2['營業收入-上月營收'].map(to_str)
df2['公司代號'] = df2['公司代號'].astype(str)
df3 = df2.set_index('公司名稱')
df3
import pandas as pd
from tabulate import tabulate
def number_add_comma(val):
# 傳入的 val 的 data type 始終為 str (原因待查)
# 故強制轉換 val 的 data type 為 int 或 float
# 若出現 exception 則傳回 -1
try:
val_dtype = type(val)
if val_dtype == str:
_val = float(val) if '.' in val else int(val)
else:
_val = val
return "{:,}".format(_val)
except Exception as e:
print(e)
return "-1"
def _main():
csv_file:str = "上市公司資料.csv"
# 依需求轉換公司代號為字串
dtype_map:dict = {
'公司代號': 'str',
}
# 依需求轉換營業收入為含逗號的字串
conv_map:dict = {
'營業收入-當月營收': number_add_comma,
'營業收入-上月營收': number_add_comma
}
csv_df:pd.DataFrame = pd.read_csv(
csv_file,
# 指定公司名稱為 index
index_col=['公司名稱'],
# 指定要顯示的欄位
usecols=['公司代號', '公司名稱', '產業別', '營業收入-當月營收', '營業收入-上月營收'],
dtype=dtype_map,
converters=conv_map)
df_cnt:int = csv_df.size
print(f"共有 {df_cnt} 筆資料")
# 因為 terminal 輸出無法像 Jupyter notebook 般的美麗
# 故用 tabulate 來美化輸出
print("\n最前五筆資料")
print(tabulate(csv_df.head(5), headers="keys", tablefmt="simple"))
# 練習用 head(), 功能同 print(tabulate(csv_df.iloc[:5,:]))
print("\n最後五筆資料")
# pandas.dataFrame.to_markdown() 是實作 tabulate 來的
print(csv_df.tail(5).to_markdown())
# 練習用 tail(), print(tabulate(csv_df.iloc[-5:,:]))
if __name__ == '__main__':
_main()
因為在 terminal 顯示的結果無法像 Jupyter notebook 般漂亮,所以用二種方式顯示結果
Note: 以上二種方式皆需要安裝 tabulate
import pandas as pd
def format_number(value):
return "{:,}".format(value)
company_data = pd.read_csv('上市公司資料.csv')
clean_data = company_data[['公司代號', '公司名稱', '產業別', '營業收入-當月營收', '營業收入-上月營收']]
format_date = clean_data.set_index('公司名稱')
format_date['公司代號'] = format_date['公司代號'].astype(str)
format_date['營業收入-當月營收'] = format_date['營業收入-當月營收'].map(format_number)
format_date['營業收入-上月營收'] = format_date['營業收入-上月營收'].map(format_number)
format_date.info()
format_date
import pandas as pd
df1 = pd.read_csv('上市公司資料.csv')
df2 = df1.set_index('公司名稱')
df3=df2.drop(columns = ['出表日期','資料年月','營業收入-去年當月營收',
'營業收入-上月比較增減(%)','營業收入-去年同月增減(%)',
'累計營業收入-當月累計營收','累計營業收入-去年累計營收',
'累計營業收入-前期比較增減(%)','備註'])
df3['公司代號'] = df3['公司代號'].astype(str)
df3['營業收入-當月營收'] = df3['營業收入-當月營收'].astype(str)
df3['營業收入-上月營收'] = df3['營業收入-上月營收'].astype(str)
df3
import pandas as pd
df1 = pd.read_csv('上市公司資料.csv')
df2 = df1.drop(columns=['營業收入-去年當月營收',
'營業收入-上月比較增減(%)',
'營業收入-去年同月增減(%)',
'累計營業收入-當月累計營收',
'累計營業收入-去年累計營收',
'累計營業收入-前期比較增減(%)',
'備註',
'出表日期',
'資料年月'])
df3 = df2.set_index('公司名稱')
# 定义转换函数
def to_str(value):
return "{:,}".format(value)
try:
return str(value)# 转换为字串
except:
return #0如果失败则返回 0
# 使用 map 函数应用 to_STR 到 '人口密度' 列
df3['營業收入-當月營收'] = df3['營業收入-當月營收'].map(to_str)
df3['營業收入-上月營收'] = df3['營業收入-上月營收'].map(to_str)
df3['公司代號'] = df3['公司代號'].map(str)
print(df3.info())
df3