我使用]构建数据框>
建立了一个html文件,没有问题result = pd.concat([dmicao,dms,dmtime,dmdt,dmwd,dmws,dmwg,dmfc,dmvis,dmch,dmcl,dmwx,dmov], axis=1)#, sort=False) headers = ['icao','msg_type','time','dt','ddd','ff','gg','flt_cat','vis','cld_hgt','cld_type','present_wx','vis_obc'] result.columns = headers icao msg_type time dt ddd ff gg flt_cat vis cld_hgt cld_type present_wx vis_obc 0 KLAX ROUTINE 2019-10-14 00:53 1:00 260 10 -9999 VFR 10.0 9999 9999 None -9999 1 KLAX ROUTINE 2019-10-14 01:53 1:00 240 9 -9999 VFR 10.0 9999 9999 None -9999 2 KLAX ROUTINE 2019-10-14 02:53 1:00 260 6 -9999 VFR 10.0 9999 9999 None -9999 3 KLAX ROUTINE 2019-10-14 03:53 1:00 250 5 -9999 VFR 10.0 9999 9999 None -9999 4 KLAX ROUTINE 2019-10-14 04:53 1:00 240 4 -9999 VFR 10.0 9999 9999 None -9999 5 KLAX ROUTINE 2019-10-14 05:53 1:00 250 5 -9999 VFR 10.0 9999 9999 None -9999
这是对象和int64的组合。我已经使用代码* pd.to_html *
我有html文件。我想根据值[]将背景cld_hgt涂上颜色
我用过
def _color_red_or_green(val): if val>2500: color = 'red' else: color = 'green' return('color: %s' % color) highlighted=df.style.apply(_color_red_or_green,subset=df['cld_hgt']) with open('table.html', 'w') as f: f.write(highlighted.render()) f.write(df)
它给出了错误。
KeyError: "None of [Int64Index([9999, 9999, 9999, 9999, 9999, 9999, 9999, 9999, 9999, 2000, 2200,\n 2200, 2200, 9999, 2500, 2500, 2700, 2600, 3000, 9999, 9999, 9999,\n 9999, 9999, 9999, 9999, 9999, 9999, 9999, 9999, 9999, 9999, 9999,\n 9999, 9999, 9999, 9999, 9999, 9999, 9999, 9999, 9999, 9999, 9999,\n 9999, 9999, 9999, 9999, 9999],\n dtype='int64')] are in the [columns]"
我查了一个类似的问题,但我无法弄清楚这个问题。我觉得这可能是我使用的代码的语法。这是类似的问题。 KeyError: "None of [['', '']] are in the [columns]" pandas python
我使用result = pd.concat([dmicao,dms,dmtime,dmdt,dmwd,dmws,dmwg,dmfc,dmvis,dmch,dmcl,dmwx,dmov],axis = 1)#,sort = False构建数据帧)标头= ['icao','msg_type','time','dt','ddd','ff'...
您的子集有误,请改用此方法。
highlighted=df.style.apply(_color_red_or_green,subset=['cld_hgt'])