Python将$numbers作为数字而不是字符串导出到excel

2024-03-29 11:31:30 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个数据帧,我需要格式化一列到$xxx.xx公司. 当我导出到excel时,我需要浮点数格式的数字,但它们是字符串。如何将它们导出为float?你知道吗

df = pd.DataFrame(index=['index1', 'index2', 'index3'], 
          columns=["A", "B", "C"], 
          data=[[5,34, "ok"], [7,8,"fine"], ["3rd",100,np.nan]])
df['B'] = (df['B']).apply(lambda x: '${:,.2f}'.format(x))
df

enter image description here

导出的excel具有字符串格式的B列:

enter image description here

我需要的是:

enter image description here


Tags: 数据字符串dataframedfindex格式公司数字