【问题标题】:Printing specific columns as a percentage以百分比形式打印特定列
【发布时间】:2020-05-10 11:49:39
【问题描述】:

我有多索引数据框,我想将两列的值转换为百分比值。

                             Capacity\nMWh  Day-Ahead\nMWh  Intraday\nMWh  UEVM\nMWh  ...  Cost Per. MW\n(with Imp.)\n$/MWh  Cost Per. MW\n(w/o Imp.)\n$/MWh  Intraday\nMape  Day-Ahead\nMape
Power Plants Date                                                                     ...
powerplant1  2020 January              3.6          446.40         492.70     482.50  ...                              0.05                             0.32            0.04             0.10
             2020 February             0.0            0.00           0.00       0.00  ...                              0.00                             0.00            0.00             0.00
             2020 March                0.0            0.00           0.00       0.00  ...                              0.00                             0.00            0.00             0.00
             2020 April                0.0            0.00           0.00       0.00  ...                              0.00                             0.00            0.00             0.00

I used apply('{:0%}'.format):

nested_df[['Intraday\nMape', 'Day-Ahead\nMape']] = \
            nested_df[['Intraday\nMape', 'Day-Ahead\nMape']].apply('{:.0%}'.format)

但是我收到了这个错误:

TypeError: ('unsupported format string passed to Series.__format__', 'occurred at index Intraday\nMape')

我该如何解决?

【问题讨论】:

    标签: pandas dataframe format percentage


    【解决方案1】:

    使用DataFrame.applymap:

    nested_df[['Intraday\nMape', 'Day-Ahead\nMape']] = \
            nested_df[['Intraday\nMape', 'Day-Ahead\nMape']].applymap('{:.0%}'.format)
    

    【讨论】:

    • 很高兴我有你,一次又一次地感谢你:))
    猜你喜欢
    • 2012-09-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-25
    • 2011-03-01
    • 2014-12-12
    • 2018-08-19
    相关资源
    最近更新 更多