Python pandas dataframe to html — highlight entire column based on header valueSplit (explode) pandas dataframe string entry to separate rowsHow to make good reproducible pandas examplesSelecting multiple columns in a pandas dataframeUse a list of values to select rows from a pandas dataframeAdding new column to existing DataFrame in Python pandasDelete column from pandas DataFrameHow to drop rows of Pandas DataFrame whose value in a certain column is NaNSelect rows from a DataFrame based on values in a column in pandasDeleting DataFrame row in Pandas based on column valuePretty-print an entire Pandas Series / DataFrameCombine two columns of text in dataframe in pandas/pythonGet list from pandas DataFrame column headers

Eliminate key lookup in execution plan

Why did I get UK entry stamps in my British passport?

Is Borg adaptation only temporary?

Can copper pour be used as an alternative to large traces?

What am I looking at here at Google Sky?

Why do presidential pardons exist in a country having a clear separation of powers?

What is the practical impact of using System.Random which is not cryptographically random?

Is this homebrew "Faerie Fire Grenade" unbalanced?

Why does the U.S. military maintain their own weather satellites?

Can inductive kick be discharged without freewheeling diode, in this example?

Can two aircraft be allowed to stay on the same runway at the same time?

Printing a list as "a, b, c." using Python

Could a complex system of reaction wheels be used to propel a spacecraft?

How can a trade secret thief avoid being caught?

How to animate a function plot

What is a "hashed transaction" in SQL Server Replication terminology?

What caused the end of cybernetic implants?

Is it good practice to speed up and slow down where not written in a song?

Group riding etiquette

“all of who” or “all of whom”?

Am I required to correct my opponent's assumptions about my morph creatures?

Should a TA point out a professor's mistake while attending their lecture?

Ask one verbal question to figure out who is blind and who is mute among three persons

Why are JWST optics not enclosed like HST?



Python pandas dataframe to html — highlight entire column based on header value


Split (explode) pandas dataframe string entry to separate rowsHow to make good reproducible pandas examplesSelecting multiple columns in a pandas dataframeUse a list of values to select rows from a pandas dataframeAdding new column to existing DataFrame in Python pandasDelete column from pandas DataFrameHow to drop rows of Pandas DataFrame whose value in a certain column is NaNSelect rows from a DataFrame based on values in a column in pandasDeleting DataFrame row in Pandas based on column valuePretty-print an entire Pandas Series / DataFrameCombine two columns of text in dataframe in pandas/pythonGet list from pandas DataFrame column headers






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








0















I have a dataframe (pulled from a dictionary) that contains the weekdays of the current week as column headers, filled with people names within the rows



I.e.



dict = '2019-03-25': ['Bob', '', 'Joe'], '2019-03-26': ['Abel', '', ''], '2019-03-27': ['', '', ''], '2019-03-28': ['', 'Bob', ''], '2019-03-29': ['Richard', 'Joe', 'Fred']

df = pd.DataFrame(data=dict)

currentdate = datetime.datetime.today().strftime('%Y-%m-%d')


I would like to highlight all of the cells in the entire column where column header = currentdate and then convert this to html



I looked elsewhere, and believe this is a unique scenario, as all I could find online was related to highlighting based on the values of the cells themselves, e.g. value is greater or less than or contains specific text, but nothing related to the value within the column header itself.



EDIT:
Basically I have a calendar in excel, which I pull into a dataframe then convert it to HTML and send it out as an email. Below is the result I've gotten to (minus the highlights). I would like to create an if statement for formatting the html, so that it highlights the column yellow when header == current date.



enter image description here










share|improve this question


























  • your example fails for me because the arrays have different length, please see here about making a good pandas question

    – philshem
    Mar 28 at 21:47












  • Apologies for the late response, something came up -- appreciate the response. You're correct in that the array were different lengths, was supposed to include blank cells in each array, to sum up to 3 per date. I have edited my question and would appreciate any assistance.

    – jpv5
    Apr 30 at 15:25

















0















I have a dataframe (pulled from a dictionary) that contains the weekdays of the current week as column headers, filled with people names within the rows



I.e.



dict = '2019-03-25': ['Bob', '', 'Joe'], '2019-03-26': ['Abel', '', ''], '2019-03-27': ['', '', ''], '2019-03-28': ['', 'Bob', ''], '2019-03-29': ['Richard', 'Joe', 'Fred']

df = pd.DataFrame(data=dict)

currentdate = datetime.datetime.today().strftime('%Y-%m-%d')


I would like to highlight all of the cells in the entire column where column header = currentdate and then convert this to html



I looked elsewhere, and believe this is a unique scenario, as all I could find online was related to highlighting based on the values of the cells themselves, e.g. value is greater or less than or contains specific text, but nothing related to the value within the column header itself.



EDIT:
Basically I have a calendar in excel, which I pull into a dataframe then convert it to HTML and send it out as an email. Below is the result I've gotten to (minus the highlights). I would like to create an if statement for formatting the html, so that it highlights the column yellow when header == current date.



enter image description here










share|improve this question


























  • your example fails for me because the arrays have different length, please see here about making a good pandas question

    – philshem
    Mar 28 at 21:47












  • Apologies for the late response, something came up -- appreciate the response. You're correct in that the array were different lengths, was supposed to include blank cells in each array, to sum up to 3 per date. I have edited my question and would appreciate any assistance.

    – jpv5
    Apr 30 at 15:25













0












0








0


1






I have a dataframe (pulled from a dictionary) that contains the weekdays of the current week as column headers, filled with people names within the rows



I.e.



dict = '2019-03-25': ['Bob', '', 'Joe'], '2019-03-26': ['Abel', '', ''], '2019-03-27': ['', '', ''], '2019-03-28': ['', 'Bob', ''], '2019-03-29': ['Richard', 'Joe', 'Fred']

df = pd.DataFrame(data=dict)

currentdate = datetime.datetime.today().strftime('%Y-%m-%d')


I would like to highlight all of the cells in the entire column where column header = currentdate and then convert this to html



I looked elsewhere, and believe this is a unique scenario, as all I could find online was related to highlighting based on the values of the cells themselves, e.g. value is greater or less than or contains specific text, but nothing related to the value within the column header itself.



EDIT:
Basically I have a calendar in excel, which I pull into a dataframe then convert it to HTML and send it out as an email. Below is the result I've gotten to (minus the highlights). I would like to create an if statement for formatting the html, so that it highlights the column yellow when header == current date.



enter image description here










share|improve this question
















I have a dataframe (pulled from a dictionary) that contains the weekdays of the current week as column headers, filled with people names within the rows



I.e.



dict = '2019-03-25': ['Bob', '', 'Joe'], '2019-03-26': ['Abel', '', ''], '2019-03-27': ['', '', ''], '2019-03-28': ['', 'Bob', ''], '2019-03-29': ['Richard', 'Joe', 'Fred']

df = pd.DataFrame(data=dict)

currentdate = datetime.datetime.today().strftime('%Y-%m-%d')


I would like to highlight all of the cells in the entire column where column header = currentdate and then convert this to html



I looked elsewhere, and believe this is a unique scenario, as all I could find online was related to highlighting based on the values of the cells themselves, e.g. value is greater or less than or contains specific text, but nothing related to the value within the column header itself.



EDIT:
Basically I have a calendar in excel, which I pull into a dataframe then convert it to HTML and send it out as an email. Below is the result I've gotten to (minus the highlights). I would like to create an if statement for formatting the html, so that it highlights the column yellow when header == current date.



enter image description here







python html pandas dataframe






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited May 6 at 22:53







jpv5

















asked Mar 27 at 23:11









jpv5jpv5

83 bronze badges




83 bronze badges















  • your example fails for me because the arrays have different length, please see here about making a good pandas question

    – philshem
    Mar 28 at 21:47












  • Apologies for the late response, something came up -- appreciate the response. You're correct in that the array were different lengths, was supposed to include blank cells in each array, to sum up to 3 per date. I have edited my question and would appreciate any assistance.

    – jpv5
    Apr 30 at 15:25

















  • your example fails for me because the arrays have different length, please see here about making a good pandas question

    – philshem
    Mar 28 at 21:47












  • Apologies for the late response, something came up -- appreciate the response. You're correct in that the array were different lengths, was supposed to include blank cells in each array, to sum up to 3 per date. I have edited my question and would appreciate any assistance.

    – jpv5
    Apr 30 at 15:25
















your example fails for me because the arrays have different length, please see here about making a good pandas question

– philshem
Mar 28 at 21:47






your example fails for me because the arrays have different length, please see here about making a good pandas question

– philshem
Mar 28 at 21:47














Apologies for the late response, something came up -- appreciate the response. You're correct in that the array were different lengths, was supposed to include blank cells in each array, to sum up to 3 per date. I have edited my question and would appreciate any assistance.

– jpv5
Apr 30 at 15:25





Apologies for the late response, something came up -- appreciate the response. You're correct in that the array were different lengths, was supposed to include blank cells in each array, to sum up to 3 per date. I have edited my question and would appreciate any assistance.

– jpv5
Apr 30 at 15:25












1 Answer
1






active

oldest

votes


















0















Note that it's not a great practice to keep data as the column name. In this case, that means the date record. It also makes the code much more complicated.



Try this code snippet -- I've added comments along the way.



import pandas as pd

# note that I've replaced '' with None
d = '2019-04-30': ['Bob', None, 'Joe'], '2019-03-26': ['Abel', None, None], '2019-03-27': [None, None, None], '2019-03-28': [None, 'Bob', None], '2019-03-29': ['Richard', 'Joe', 'Fred']

# load items of dictionary as dataframe, don't use dates as column names, assign column names
df = pd.DataFrame(data=d.items(), columns = ['date','name_list'])

# convert string to datetime
# it's better to compare datetimes directly, and forces the type to datetime, in case of dirty data
# also using pandas datetime instead of the standard python library
df.date = pd.to_datetime(df.date)

# this is your filter or 'where' clause, to keep only records that match today
df = df.loc[df['date'] == pd.to_datetime('today')]

# unpack list as individual rows, see https://stackoverflow.com/a/12681217/2327328
df = pd.concat([pd.Series(row['date'], row['name_list'])
for _, row in df.iterrows()]).reset_index()


# display as html
print (df.to_html())


Provides as an output:



<table border="1" class="dataframe">
<thead>
<tr style="text-align: right;">
<th></th>
<th>index</th>
<th>0</th>
</tr>
</thead>
<tbody>
<tr>
<th>0</th>
<td>Bob</td>
<td>2019-04-30</td>
</tr>
<tr>
<th>1</th>
<td>None</td>
<td>2019-04-30</td>
</tr>
<tr>
<th>2</th>
<td>Joe</td>
<td>2019-04-30</td>
</tr>
</tbody>
</table>


which looks displays this:



enter image description here






share|improve this answer

























  • Appreciate the quick response, and your solution. However, I edited my original question as I believe I wasn't clear enough. I would like to keep all columns of the dataframe in html, I do not want to filter out only data pertaining to today, but highlight data pertaining to today. Thanks!

    – jpv5
    May 6 at 14:38











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55387873%2fpython-pandas-dataframe-to-html-highlight-entire-column-based-on-header-value%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0















Note that it's not a great practice to keep data as the column name. In this case, that means the date record. It also makes the code much more complicated.



Try this code snippet -- I've added comments along the way.



import pandas as pd

# note that I've replaced '' with None
d = '2019-04-30': ['Bob', None, 'Joe'], '2019-03-26': ['Abel', None, None], '2019-03-27': [None, None, None], '2019-03-28': [None, 'Bob', None], '2019-03-29': ['Richard', 'Joe', 'Fred']

# load items of dictionary as dataframe, don't use dates as column names, assign column names
df = pd.DataFrame(data=d.items(), columns = ['date','name_list'])

# convert string to datetime
# it's better to compare datetimes directly, and forces the type to datetime, in case of dirty data
# also using pandas datetime instead of the standard python library
df.date = pd.to_datetime(df.date)

# this is your filter or 'where' clause, to keep only records that match today
df = df.loc[df['date'] == pd.to_datetime('today')]

# unpack list as individual rows, see https://stackoverflow.com/a/12681217/2327328
df = pd.concat([pd.Series(row['date'], row['name_list'])
for _, row in df.iterrows()]).reset_index()


# display as html
print (df.to_html())


Provides as an output:



<table border="1" class="dataframe">
<thead>
<tr style="text-align: right;">
<th></th>
<th>index</th>
<th>0</th>
</tr>
</thead>
<tbody>
<tr>
<th>0</th>
<td>Bob</td>
<td>2019-04-30</td>
</tr>
<tr>
<th>1</th>
<td>None</td>
<td>2019-04-30</td>
</tr>
<tr>
<th>2</th>
<td>Joe</td>
<td>2019-04-30</td>
</tr>
</tbody>
</table>


which looks displays this:



enter image description here






share|improve this answer

























  • Appreciate the quick response, and your solution. However, I edited my original question as I believe I wasn't clear enough. I would like to keep all columns of the dataframe in html, I do not want to filter out only data pertaining to today, but highlight data pertaining to today. Thanks!

    – jpv5
    May 6 at 14:38
















0















Note that it's not a great practice to keep data as the column name. In this case, that means the date record. It also makes the code much more complicated.



Try this code snippet -- I've added comments along the way.



import pandas as pd

# note that I've replaced '' with None
d = '2019-04-30': ['Bob', None, 'Joe'], '2019-03-26': ['Abel', None, None], '2019-03-27': [None, None, None], '2019-03-28': [None, 'Bob', None], '2019-03-29': ['Richard', 'Joe', 'Fred']

# load items of dictionary as dataframe, don't use dates as column names, assign column names
df = pd.DataFrame(data=d.items(), columns = ['date','name_list'])

# convert string to datetime
# it's better to compare datetimes directly, and forces the type to datetime, in case of dirty data
# also using pandas datetime instead of the standard python library
df.date = pd.to_datetime(df.date)

# this is your filter or 'where' clause, to keep only records that match today
df = df.loc[df['date'] == pd.to_datetime('today')]

# unpack list as individual rows, see https://stackoverflow.com/a/12681217/2327328
df = pd.concat([pd.Series(row['date'], row['name_list'])
for _, row in df.iterrows()]).reset_index()


# display as html
print (df.to_html())


Provides as an output:



<table border="1" class="dataframe">
<thead>
<tr style="text-align: right;">
<th></th>
<th>index</th>
<th>0</th>
</tr>
</thead>
<tbody>
<tr>
<th>0</th>
<td>Bob</td>
<td>2019-04-30</td>
</tr>
<tr>
<th>1</th>
<td>None</td>
<td>2019-04-30</td>
</tr>
<tr>
<th>2</th>
<td>Joe</td>
<td>2019-04-30</td>
</tr>
</tbody>
</table>


which looks displays this:



enter image description here






share|improve this answer

























  • Appreciate the quick response, and your solution. However, I edited my original question as I believe I wasn't clear enough. I would like to keep all columns of the dataframe in html, I do not want to filter out only data pertaining to today, but highlight data pertaining to today. Thanks!

    – jpv5
    May 6 at 14:38














0














0










0









Note that it's not a great practice to keep data as the column name. In this case, that means the date record. It also makes the code much more complicated.



Try this code snippet -- I've added comments along the way.



import pandas as pd

# note that I've replaced '' with None
d = '2019-04-30': ['Bob', None, 'Joe'], '2019-03-26': ['Abel', None, None], '2019-03-27': [None, None, None], '2019-03-28': [None, 'Bob', None], '2019-03-29': ['Richard', 'Joe', 'Fred']

# load items of dictionary as dataframe, don't use dates as column names, assign column names
df = pd.DataFrame(data=d.items(), columns = ['date','name_list'])

# convert string to datetime
# it's better to compare datetimes directly, and forces the type to datetime, in case of dirty data
# also using pandas datetime instead of the standard python library
df.date = pd.to_datetime(df.date)

# this is your filter or 'where' clause, to keep only records that match today
df = df.loc[df['date'] == pd.to_datetime('today')]

# unpack list as individual rows, see https://stackoverflow.com/a/12681217/2327328
df = pd.concat([pd.Series(row['date'], row['name_list'])
for _, row in df.iterrows()]).reset_index()


# display as html
print (df.to_html())


Provides as an output:



<table border="1" class="dataframe">
<thead>
<tr style="text-align: right;">
<th></th>
<th>index</th>
<th>0</th>
</tr>
</thead>
<tbody>
<tr>
<th>0</th>
<td>Bob</td>
<td>2019-04-30</td>
</tr>
<tr>
<th>1</th>
<td>None</td>
<td>2019-04-30</td>
</tr>
<tr>
<th>2</th>
<td>Joe</td>
<td>2019-04-30</td>
</tr>
</tbody>
</table>


which looks displays this:



enter image description here






share|improve this answer













Note that it's not a great practice to keep data as the column name. In this case, that means the date record. It also makes the code much more complicated.



Try this code snippet -- I've added comments along the way.



import pandas as pd

# note that I've replaced '' with None
d = '2019-04-30': ['Bob', None, 'Joe'], '2019-03-26': ['Abel', None, None], '2019-03-27': [None, None, None], '2019-03-28': [None, 'Bob', None], '2019-03-29': ['Richard', 'Joe', 'Fred']

# load items of dictionary as dataframe, don't use dates as column names, assign column names
df = pd.DataFrame(data=d.items(), columns = ['date','name_list'])

# convert string to datetime
# it's better to compare datetimes directly, and forces the type to datetime, in case of dirty data
# also using pandas datetime instead of the standard python library
df.date = pd.to_datetime(df.date)

# this is your filter or 'where' clause, to keep only records that match today
df = df.loc[df['date'] == pd.to_datetime('today')]

# unpack list as individual rows, see https://stackoverflow.com/a/12681217/2327328
df = pd.concat([pd.Series(row['date'], row['name_list'])
for _, row in df.iterrows()]).reset_index()


# display as html
print (df.to_html())


Provides as an output:



<table border="1" class="dataframe">
<thead>
<tr style="text-align: right;">
<th></th>
<th>index</th>
<th>0</th>
</tr>
</thead>
<tbody>
<tr>
<th>0</th>
<td>Bob</td>
<td>2019-04-30</td>
</tr>
<tr>
<th>1</th>
<td>None</td>
<td>2019-04-30</td>
</tr>
<tr>
<th>2</th>
<td>Joe</td>
<td>2019-04-30</td>
</tr>
</tbody>
</table>


which looks displays this:



enter image description here







share|improve this answer












share|improve this answer



share|improve this answer










answered Apr 30 at 20:25









philshemphilshem

18.3k5 gold badges36 silver badges95 bronze badges




18.3k5 gold badges36 silver badges95 bronze badges















  • Appreciate the quick response, and your solution. However, I edited my original question as I believe I wasn't clear enough. I would like to keep all columns of the dataframe in html, I do not want to filter out only data pertaining to today, but highlight data pertaining to today. Thanks!

    – jpv5
    May 6 at 14:38


















  • Appreciate the quick response, and your solution. However, I edited my original question as I believe I wasn't clear enough. I would like to keep all columns of the dataframe in html, I do not want to filter out only data pertaining to today, but highlight data pertaining to today. Thanks!

    – jpv5
    May 6 at 14:38

















Appreciate the quick response, and your solution. However, I edited my original question as I believe I wasn't clear enough. I would like to keep all columns of the dataframe in html, I do not want to filter out only data pertaining to today, but highlight data pertaining to today. Thanks!

– jpv5
May 6 at 14:38






Appreciate the quick response, and your solution. However, I edited my original question as I believe I wasn't clear enough. I would like to keep all columns of the dataframe in html, I do not want to filter out only data pertaining to today, but highlight data pertaining to today. Thanks!

– jpv5
May 6 at 14:38









Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.







Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.



















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55387873%2fpython-pandas-dataframe-to-html-highlight-entire-column-based-on-header-value%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

은진 송씨 목차 역사 본관 분파 인물 조선 왕실과의 인척 관계 집성촌 항렬자 인구 같이 보기 각주 둘러보기 메뉴은진 송씨세종실록 149권, 지리지 충청도 공주목 은진현