Pandas read_excel percentages as strings

a 夏天 提交于 2021-02-07 22:47:16

问题


My excel sheet has a column of percentages stored with the percent symbol (eg "50%"). How can I coerce pandas.read_excel to read the string "50%" instead of casting it to a float?

Currently the read_excel implementation parses the percentage into the float 0.5. Additionally if I add a converter = {col_with_percentage: str} argument, it parses it into the string '0.5'. Is there a way to read the raw percentage value ("50%")?


回答1:


You can pass your own function with the converters. Something to make a string (eg: 50%) could look like:

Code:

def convert_to_percent_string(value):
    return '{}%'.format(value * 100)

Test Code:

import pandas as pd
df = pd.read_excel('example.xlsx', converters={
    'percents': convert_to_percent_string})
print(df)

Or as a lambda:

df = pd.read_excel('example.xlsx', converters={
    'percents': lambda value: '{}%'.format(value * 100)})

Results:

  percents
0    40.0%
1    50.0%
2    60.0%



回答2:


You can generate string after reading

df = pd.DataFrame(np.random.ranf(size=(4,1)),columns =['col_with_percentage'])
df['col_with_percentage_s']= (df.col_with_percentage*100).astype(int).astype(str)+'%'
df                 

Output:

    col_with_percentage col_with_percentage_s
0   0.5339712650806299  53%
1   0.9220323933894158  92%
2   0.11156261877930995 11%
3   0.18864363985224808 18%

But better way is to format on display, you can do it by style in pandas

df.style.format({'col_with_percentage': "{:.0%}"})

Output:

col_with_percentage col_with_percentage_s
0   53% 53%
1   92% 92%
2   11% 11%
3   19% 18%



来源:https://stackoverflow.com/questions/48977649/pandas-read-excel-percentages-as-strings

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!