如果值在熊猫中是浮点数(不是字符串),则将列除以其他列?

时间:2018-10-11 07:32:37

标签: python pandas

我有以下数据框并尝试应用以下命令。但是,由于每一列都包含某一行的字符串,因此出现错误。谁能告诉我如果值是字符串,如何修改此脚本以跳过计算?

df["diff"] = (df["value"] / df["value_B"] - 1) * 100  

数据框

                                                        value    value_B
GeneralEUI[kWh/m2]EUI[kWh/m2]EUI[kWh/m2]                217.3      243.3
GeneralTotal Unmer Hour[h]Total Unmer Hour[h]To...        536        536
GeneralTotal Area[m2]Total Area[m2]Total Area[m2]       51513      51513
GeneralLocationLocationLocation                     Hong Kong  Hong Kong
GeneralProgramProgramProgram                           Office     Office
EnvelopeTotal Window to Wall Ratio [%]Total Win...      52.23      52.23
EnvelopeGlassSHGC value                                  0.24       0.24
EnvelopeGlassU-value[W/m2-K]                             1.74       1.74
EnvelopeGlassOFFICE WINDOWSHGC value                     0.22       0.22
EnvelopeGlassOFFICE WINDOWU-value[W/m2-K]                1.63       1.63
EnvelopeGlassPODIUM WINDOWSHGC value                      0.7        0.7
EnvelopeGlassPODIUM WINDOWU-value[W/m2-K]                5.48       5.48
EnvelopeOpaqueSHGC value                                 0.24       0.24
EnvelopeOpaqueU-value[W/m2-K]                            1.74       1.74
EnvelopeOpaqueOFFICE WINDOWSHGC value                    0.22       0.22

1 个答案:

答案 0 :(得分:1)

您可以通过to_numeric使用参数errors='coerce'将字符串转换为缺少的值:

a = pd.to_numeric(df["value"], errors='coerce')
b = pd.to_numeric(df["value_B"], errors='coerce')

df["diff"] =  (a / b - 1) * 100  

print (df)
                                                        value    value_B  \
GeneralEUI[kWh/m2]EUI[kWh/m2]EUI[kWh/m2]                217.3      243.3   
GeneralTotal Unmer Hour[h]Total Unmer Hour[h]To...        536        536   
GeneralTotal Area[m2]Total Area[m2]Total Area[m2]       51513      51513   
GeneralLocationLocationLocation                     Hong Kong  Hong Kong   
GeneralProgramProgramProgram                           Office     Office   
EnvelopeTotal Window to Wall Ratio [%]Total Win...      52.23      52.23   
EnvelopeGlassSHGC value                                  0.24       0.24   
EnvelopeGlassU-value[W/m2-K]                             1.74       1.74   
EnvelopeGlassOFFICE WINDOWSHGC value                     0.22       0.22   
EnvelopeGlassOFFICE WINDOWU-value[W/m2-K]                1.63       1.63   
EnvelopeGlassPODIUM WINDOWSHGC value                      0.7        0.7   
EnvelopeGlassPODIUM WINDOWU-value[W/m2-K]                5.48       5.48   
EnvelopeOpaqueSHGC value                                 0.24       0.24   
EnvelopeOpaqueU-value[W/m2-K]                            1.74       1.74   
EnvelopeOpaqueOFFICE WINDOWSHGC value                    0.22       0.22   

                                                         diff  
GeneralEUI[kWh/m2]EUI[kWh/m2]EUI[kWh/m2]           -10.686395  
GeneralTotal Unmer Hour[h]Total Unmer Hour[h]To...   0.000000  
GeneralTotal Area[m2]Total Area[m2]Total Area[m2]    0.000000  
GeneralLocationLocationLocation                           NaN  
GeneralProgramProgramProgram                              NaN  
EnvelopeTotal Window to Wall Ratio [%]Total Win...   0.000000  
EnvelopeGlassSHGC value                              0.000000  
EnvelopeGlassU-value[W/m2-K]                         0.000000  
EnvelopeGlassOFFICE WINDOWSHGC value                 0.000000  
EnvelopeGlassOFFICE WINDOWU-value[W/m2-K]            0.000000  
EnvelopeGlassPODIUM WINDOWSHGC value                 0.000000  
EnvelopeGlassPODIUM WINDOWU-value[W/m2-K]            0.000000  
EnvelopeOpaqueSHGC value                             0.000000  
EnvelopeOpaqueU-value[W/m2-K]                        0.000000  
EnvelopeOpaqueOFFICE WINDOWSHGC value                0.000000