Pandas to_mardown() highlight row - python

Is it possible to change the text color for a specific row when using df.to_markdown()?
I have attempted using df.style.apply([styles]).to_markdown() but does does not work since it converts the df into an styler object.
code:
print(labels_data.to_markdown(headers= df.columns, tablefmt="jira", showindex=True))
df styled code:
def row_name(x):
for i in x:
if x.name=='Control':
return ['background: yellow']
else:
return ''
labels_data = count_labels(labels).sort_values('% meets', ascending=False)
labels_data.style.apply(lambda x: ['background: lightgreen'
if (x.name == 'control')
else '' for i in x], axis=1)
My use case is printing a markdown to copy paste a table (with styles) in jira.
Thanks

Why not use html? Html is valid inline within markdown, but there isn't a standard way of marking rows within a markdown table (not that markdown is a much of standard, in practice, anyway).

Related

converting links data from list to series changes the links inside the final series (I don't want it clickable just the data frame to be perfect) [duplicate]

print('http://google.com') outputs a clickable url.
How do I get clickable URLs for pd.DataFrame(['http://google.com', 'http://duckduckgo.com']) ?
If you want to apply URL formatting only to a single column, you can use:
data = [dict(name='Google', url='http://www.google.com'),
dict(name='Stackoverflow', url='http://stackoverflow.com')]
df = pd.DataFrame(data)
def make_clickable(val):
# target _blank to open new window
return '<a target="_blank" href="{}">{}</a>'.format(val, val)
df.style.format({'url': make_clickable})
(PS: Unfortunately, I didn't have enough reputation to post this as a comment to #Abdou's post)
Try using pd.DataFrame.style.format for this:
df = pd.DataFrame(['http://google.com', 'http://duckduckgo.com'])
def make_clickable(val):
return '{}'.format(val,val)
df.style.format(make_clickable)
I hope this proves useful.
#shantanuo : not enough reputation to comment.
How about the following?
def make_clickable(url, name):
return '{}'.format(url,name)
df['name'] = df.apply(lambda x: make_clickable(x['url'], x['name']), axis=1)
I found this at How to Create a Clickable Link(s) in Pandas DataFrame and JupyterLab which solved my problem:
HTML(df.to_html(render_links=True, escape=False))
from IPython.core.display import display, HTML
import pandas as pd
# create a table with a url column
df = pd.DataFrame({"url": ["http://google.com", "http://duckduckgo.com"]})
# create the column clickable_url based on the url column
df["clickable_url"] = df.apply(lambda row: "<a href='{}' target='_blank'>{}</a>".format(row.url, row.url.split("/")[2]), axis=1)
# display the table as HTML. Note, only the clickable_url is being selected here
display(HTML(df[["clickable_url"]].to_html(escape=False)))

Highlight a column incl. header with style in Pivottable [duplicate]

I am using pandas styler to give some columns a background color, based on the name of the column header.
While this works as intended, the background color of the column header doesn't change.
Here is the part in my script where thy style is applied:
def highlight_col(x):
if x.name in added_columns:
return ['background-color: #67c5a4']*x.shape[0]
elif x.name in dropped_columns:
return ['background-color: #ff9090']*x.shape[0]
else:
return ['background-color: None']*x.shape[0]
old = old.style.apply(highlight_col, axis=0)
Is there a way to apply the style.apply()-function not only to the cells below the column header, but the complete column including the column header?
Edit:
For clarification here is a screenshot of the excel output:
screenshot of excel output
"Header 2" should have the same background color as the cells below it.
Okay, I think I figured out a way to handle formatting a column header using html 'selectors':
Using much of your code as setup:
df = pd.DataFrame('some value', columns=['Header1','Header2','Header3'], index=np.arange(12))
added_columns = 'Header2'
dropped_columns = 'Header1'
def highlight_col(x):
if x.name in added_columns:
return ['background-color: #67c5a4']*x.shape[0]
elif x.name in dropped_columns:
return ['background-color: #ff9090']*x.shape[0]
else:
return ['background-color: None']*x.shape[0]
col_loc_add = df.columns.get_loc(added_columns) + 2
col_loc_drop = df.columns.get_loc(dropped_columns) + 2
df.style.apply(highlight_col, axis=0)\
.set_table_styles(
[{'selector': f'th:nth-child({col_loc_add})',
'props': [('background-color', '#67c5a4')]},
{'selector': f'th:nth-child({col_loc_drop})',
'props': [('background-color', '#ff9090')]}])
Output:
Note: I am using f-string which is a Python 3.6+ feature.
You can use np.vstack() to stack the column names like below and create a fresh dataframe to apply the function, then export to excel with header=False:
Using #Scott's data and piR's function,
Setup:
df = pd.DataFrame('some value', columns=['Header1','Header2','Header3'], index=np.arange(12))
def f(dat, c='red'):
return [f'background-color: {c}' for i in dat]
You can do:
pd.DataFrame(np.vstack((df.columns,df.to_numpy())),columns=df.columns).style.apply(
f,subset=['Header2']).to_excel('file.xlsx',header=False,index=False)
Output of excel file:

Pandas 'None' type does not apply to all empty columns

I have some code that uses the 'openpyexcel' package to read Excel spreadsheets from a file. I need to know which columns are empty, but have found that for some reason not all empty fields are 'None' type and instead just appear blank. I've been trying to clear out whatever is in there using .replace, but haven't had much luck. I'm not sure if my regular expression is wrong or just doesn't apply to something in the column. I'm still working getting comfortable with Numpy data types and am a little lost.
Regrex I've been trying to use to replace the invisible data:
df = df.replace(r'^[?!a-zA-Z0-9_]+$', r'', regex=True)
Function to create a list of empty columns:
def lister(table, table_name):
try:
lst = []
table.replace(to_replace='', value= None)
for c in table.columns:
if c == 'NOT USED':
continue
elif (table[c].isna().all()) == True:
print(c)
lst.append(c)
else:
continue
return lst

Python Pandas: Style column header

I am using pandas styler to give some columns a background color, based on the name of the column header.
While this works as intended, the background color of the column header doesn't change.
Here is the part in my script where thy style is applied:
def highlight_col(x):
if x.name in added_columns:
return ['background-color: #67c5a4']*x.shape[0]
elif x.name in dropped_columns:
return ['background-color: #ff9090']*x.shape[0]
else:
return ['background-color: None']*x.shape[0]
old = old.style.apply(highlight_col, axis=0)
Is there a way to apply the style.apply()-function not only to the cells below the column header, but the complete column including the column header?
Edit:
For clarification here is a screenshot of the excel output:
screenshot of excel output
"Header 2" should have the same background color as the cells below it.
Okay, I think I figured out a way to handle formatting a column header using html 'selectors':
Using much of your code as setup:
df = pd.DataFrame('some value', columns=['Header1','Header2','Header3'], index=np.arange(12))
added_columns = 'Header2'
dropped_columns = 'Header1'
def highlight_col(x):
if x.name in added_columns:
return ['background-color: #67c5a4']*x.shape[0]
elif x.name in dropped_columns:
return ['background-color: #ff9090']*x.shape[0]
else:
return ['background-color: None']*x.shape[0]
col_loc_add = df.columns.get_loc(added_columns) + 2
col_loc_drop = df.columns.get_loc(dropped_columns) + 2
df.style.apply(highlight_col, axis=0)\
.set_table_styles(
[{'selector': f'th:nth-child({col_loc_add})',
'props': [('background-color', '#67c5a4')]},
{'selector': f'th:nth-child({col_loc_drop})',
'props': [('background-color', '#ff9090')]}])
Output:
Note: I am using f-string which is a Python 3.6+ feature.
You can use np.vstack() to stack the column names like below and create a fresh dataframe to apply the function, then export to excel with header=False:
Using #Scott's data and piR's function,
Setup:
df = pd.DataFrame('some value', columns=['Header1','Header2','Header3'], index=np.arange(12))
def f(dat, c='red'):
return [f'background-color: {c}' for i in dat]
You can do:
pd.DataFrame(np.vstack((df.columns,df.to_numpy())),columns=df.columns).style.apply(
f,subset=['Header2']).to_excel('file.xlsx',header=False,index=False)
Output of excel file:

Python Excel Highlight Cell Differences

preface: I'm new and self taught. This is my first coding project. I know it's terrible. I'm going to rewrite it once it's complete and working.
I'm trying to write a python script that will compare 2 excel files and highlight the cells that are different. I can print out the differences (using pandas) and highlight a cell (only by hard coding a specific cell). I can't figure out how to highlight cells based on the printed out differences.
df1 = pd.read_excel(mxln) # Loads master xlsx for comparison
df2 = pd.read_excel(sfcn) # Loads student xlsx for comparison
print('If NaN, correct. If not NaN, incorrect')
difference = df2[df2 != df1] # Scans for differences
print(difference)
lmfh = load_workbook(mxln) # Load mxln for highlight
lsfh = load_workbook(sfcn) # Load sfcn for highlight
lmws = lmfh.active
lsws = lsfh.active
redFill = PatternFill(start_color='FFEE1111', end_color='FFEE1111', fill_type='solid')
lsws['A1'].fill = redFill # Hardcoded cell color
lsfh.save(sfcn)
This is only the part of the code I'm stuck on. I can post the rest if necessary.
You can use the style to add highlighting to your dataframe in pandas.
df2.style.apply(highlight_differences)
Then you can write a function which sets the highlighting criteria
def highlight_differences():
# check for differences here
return ['background-color: yellow']

Categories