程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

How to solve the format problem of pandas output?

編輯:Python

一個表格中,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?



其他答案1:

10000Unlikely to be converted to via a table“萬元”單位的值,You can only manually format and convert it before writing.


其他答案2:
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)


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved