Pandas通过保存格式来读取excel数据

2024-04-25 09:46:54 发布

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

我的excel文件包含以下数据

df=3%   6%  7%  8%  7%

当我用pd.read\u excel阅读时

df=0.03 0.06 0.07 0.08 0.07

是否有办法读取数据,以便保留%符号? 提前谢谢


Tags: 文件数据dfread符号读取数据excelpd
1条回答
网友
1楼 · 发布于 2024-04-25 09:46:54

IIUC,我们可以使用pandas.style.format

实际上,pandas正在正确读取这些值,因为您的单元格顶部有一个样式化的版本,如果您检查单元格,您将看到如下内容:

enter image description here

通常不建议出于表示目的而更改原始数据,使用样式或html方法

import pandas as pd

df = pd.DataFrame({'foo' : [0.2,0.4], 'bar' : [0.6, 0.82]})

print(df)
   foo   bar
0  0.2  0.60
1  0.4  0.82


df.style.format({
    'foo': '{:.0%}'.format,
    'bar' : '{:.1%}'.format
})


    foo     bar
0   20%     60.0%
1   40%     82.0%

相关问题 更多 >