i'm trying use formatters argument add html property specific cell/text. i've found answer fits request. trying it, results in returning none. example i'm trying:
import pandas pd stringio import stringio buf = stringio() significant = lambda x: '<span class="significant">%f</span>' % x if x<0.05 else str(x) df = pd.dataframe({'correlation':[0.5, 0.1,0.9], 'p_value':[0.1,0.8,0.01]}) print df.to_html(buf, formatters={'p_value': significant}, escape=false)
this question 2013. syntax changed 1 version another? i'm using python python 2.7.9 pandas 0.16.0.
you have given dataframe.to_html function stringio buffer, means result written it, not returned string.
this has not changed since pandas version 0.10 of dataframe.to_html() function
to output returned string, remove buffer:
import pandas pd df = pd.dataframe( {'correlation':[0.5, 0.1,0.9], 'p_value':[0.1,0.8,0.01]}) strformat = '<span class="significant">%f</span>' significant = lambda x: strformat % x if x<0.05 else str(x) formatters = {'p_value': significant} print df.to_html(formatters=formatters, escape=false)
or, if want use buffer, how print value of it:
from stringio import stringio import pandas pd df = pd.dataframe( {'correlation':[0.5, 0.1,0.9], 'p_value':[0.1,0.8,0.01]}) strformat = '<span class="significant">%f</span>' significant = lambda x: strformat % x if x<0.05 else str(x) formatters = {'p_value': significant} buf = stringio() df.to_html(buf, formatters=formatters, escape=false) print buf.getvalue()
Comments
Post a Comment