一個表格中,If you want to output the results according to the type shown as shown below
應該怎麼設置format?
數據如下:
import pandas as pd data=[["A",10000,5000,0.5],["B",20000,30000,1.5],["C",30000,10000,0.3333333]] dt=pd.DataFrame(data,columns=["產品","任務","銷售","完成率"])
Except for formatting the columns individually,Is there a way to set this all at once,
按照字符串、整形、Floating point formats the data?
dt=dt.apply(lambda x : format(x,".2%") if pd.api.types.is_float_dtype(x) else (str(round(x/10000,1)) + "萬" if pd.api.types.is_integer(x) else x),axis=1)
This part can run,But totally useless,I would like to ask if it can be used to determine the number,Re-output format?
10000Unlikely to be converted to via a table“萬元”單位的值,You can only manually format and convert it before writing.
dt = dt.applymap(lambda x: format(x, ".2%") if pd.api.types.is_float(x) else '{:.1f}萬'.format(x/10000) if pd.api.types.is_integer(x) else x)