You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Visualization of pd.DataFrame as a Markdown format
Useful to generate markdown document
[ NOTICE ] the output of the function is strongly influenced by markdown characters, such as |, *, ~. The output of the df2md should be used only for visualization, not as input to another function.
# Nice representation of dataframe in markdownimportpandasaspdimportcopyfromIPython.displayimportMarkdown, displayimportwarningswarnings.filterwarnings(action='ignore')
defdf2md(df, maxlen=20, indexname='(index)'):
''' Nice Representation of Pandas DataFrame and Series in Markdown Format. Parameters ---------------- df : pandas.DataFrame or pandas.Series maxlen : (int) maximum length of data at markdown output. data exceeding maxlen will be presented as ' ...' indexname : (str) name of index, to be displayed on markdown output. To avoid overriding column name, if conflicts, the '_' will be added in front of the indexname. '''_df=copy.deepcopy(df)
if'Series'instr(type(df)):
_idx=_df.index_df=pd.DataFrame(data=_df, index=_idx)
forcolin_df.columns:
_df[col] =_df[col].astype('str')
if (_df[col].str.len()>maxlen).any() :
_df[col].loc[_df[col].str.len() >maxlen] =_df[col].str.slice(stop=maxlen) +' ...'ifindexnamein_df.columns:
while(indexnamein_df.columns):
indexname='_'+indexnamewarnings.warn("The index name shouldn't overlap other column names. {} will be used instead.\nConsider changing the indexname parameter.".format(indexname), SyntaxWarning)
_df.insert(0, indexname, df.index)
fmt= ['---'foriinrange(len(_df.columns))]
df_fmt=pd.DataFrame([fmt], columns=_df.columns)
df_formatted=pd.concat([df_fmt, _df])
display(Markdown(df_formatted.to_csv(sep='|', index=False)))
_df.drop(columns=indexname, axis=1, inplace=True)