Rotating the column name for a Panda DataFrame
I'm trying to make nicely formatted tables from pandas. Some of my column names are far too long. The cells for these columns are large cause the whole table to be a mess.
In my example, is it possible to rotate the column names as they are displayed?
data = [{'Way too long of a column to be reasonable':4,'Four?':4},
{'Way too long of a column to be reasonable':5,'Four?':5}]
pd.DataFrame(data)
python pandas dataframe jupyter-notebook
add a comment |
I'm trying to make nicely formatted tables from pandas. Some of my column names are far too long. The cells for these columns are large cause the whole table to be a mess.
In my example, is it possible to rotate the column names as they are displayed?
data = [{'Way too long of a column to be reasonable':4,'Four?':4},
{'Way too long of a column to be reasonable':5,'Four?':5}]
pd.DataFrame(data)
python pandas dataframe jupyter-notebook
1
could you please added tagjupyter notebook
– Wen-Ben
Oct 12 '17 at 18:24
Obviously this depends on your situation, but maybe it makes sense to just shorten/improve the column names? But for a workaround, you could consider converting to a "fake" multi-index. E.g. if column name is "david s pumpkins" you could have "david s" as the top level and "pumpkins" as the second level. Obviously that's not what multi-indexes are for, but I think it would basically do what you want.
– JohnE
Oct 13 '17 at 2:37
@JohnE, Thanks for the suggestion. A text wrap is what @Wen had suggested which is deleted but was:df.style.set_table_styles([dict(selector="th",props=[('max-width', '50px')])])
It's a good work-around that works for phrases, like in my example. I also have long words though. I'm still curious to see if rotation is possible! I'm no CSS selector expert, but I thought this should work but doesn't:dfoo.style.set_table_styles([dict(selector="th",props=[('text-orientation', 'upright')])])
– Dan Fiorino
Oct 13 '17 at 15:49
I just re-read the doc page (pandas.pydata.org/pandas-docs/version/0.19.2/style.html) which states that 'You can only style the values, not the index or columns', but the doc shows columns and indices being formatted. Maybe this is is just starting to be addressed.
– Dan Fiorino
Oct 13 '17 at 16:18
add a comment |
I'm trying to make nicely formatted tables from pandas. Some of my column names are far too long. The cells for these columns are large cause the whole table to be a mess.
In my example, is it possible to rotate the column names as they are displayed?
data = [{'Way too long of a column to be reasonable':4,'Four?':4},
{'Way too long of a column to be reasonable':5,'Four?':5}]
pd.DataFrame(data)
python pandas dataframe jupyter-notebook
I'm trying to make nicely formatted tables from pandas. Some of my column names are far too long. The cells for these columns are large cause the whole table to be a mess.
In my example, is it possible to rotate the column names as they are displayed?
data = [{'Way too long of a column to be reasonable':4,'Four?':4},
{'Way too long of a column to be reasonable':5,'Four?':5}]
pd.DataFrame(data)
python pandas dataframe jupyter-notebook
python pandas dataframe jupyter-notebook
edited Oct 12 '17 at 19:25
Dan Fiorino
asked Oct 12 '17 at 17:38
Dan FiorinoDan Fiorino
9511
9511
1
could you please added tagjupyter notebook
– Wen-Ben
Oct 12 '17 at 18:24
Obviously this depends on your situation, but maybe it makes sense to just shorten/improve the column names? But for a workaround, you could consider converting to a "fake" multi-index. E.g. if column name is "david s pumpkins" you could have "david s" as the top level and "pumpkins" as the second level. Obviously that's not what multi-indexes are for, but I think it would basically do what you want.
– JohnE
Oct 13 '17 at 2:37
@JohnE, Thanks for the suggestion. A text wrap is what @Wen had suggested which is deleted but was:df.style.set_table_styles([dict(selector="th",props=[('max-width', '50px')])])
It's a good work-around that works for phrases, like in my example. I also have long words though. I'm still curious to see if rotation is possible! I'm no CSS selector expert, but I thought this should work but doesn't:dfoo.style.set_table_styles([dict(selector="th",props=[('text-orientation', 'upright')])])
– Dan Fiorino
Oct 13 '17 at 15:49
I just re-read the doc page (pandas.pydata.org/pandas-docs/version/0.19.2/style.html) which states that 'You can only style the values, not the index or columns', but the doc shows columns and indices being formatted. Maybe this is is just starting to be addressed.
– Dan Fiorino
Oct 13 '17 at 16:18
add a comment |
1
could you please added tagjupyter notebook
– Wen-Ben
Oct 12 '17 at 18:24
Obviously this depends on your situation, but maybe it makes sense to just shorten/improve the column names? But for a workaround, you could consider converting to a "fake" multi-index. E.g. if column name is "david s pumpkins" you could have "david s" as the top level and "pumpkins" as the second level. Obviously that's not what multi-indexes are for, but I think it would basically do what you want.
– JohnE
Oct 13 '17 at 2:37
@JohnE, Thanks for the suggestion. A text wrap is what @Wen had suggested which is deleted but was:df.style.set_table_styles([dict(selector="th",props=[('max-width', '50px')])])
It's a good work-around that works for phrases, like in my example. I also have long words though. I'm still curious to see if rotation is possible! I'm no CSS selector expert, but I thought this should work but doesn't:dfoo.style.set_table_styles([dict(selector="th",props=[('text-orientation', 'upright')])])
– Dan Fiorino
Oct 13 '17 at 15:49
I just re-read the doc page (pandas.pydata.org/pandas-docs/version/0.19.2/style.html) which states that 'You can only style the values, not the index or columns', but the doc shows columns and indices being formatted. Maybe this is is just starting to be addressed.
– Dan Fiorino
Oct 13 '17 at 16:18
1
1
could you please added tag
jupyter notebook
– Wen-Ben
Oct 12 '17 at 18:24
could you please added tag
jupyter notebook
– Wen-Ben
Oct 12 '17 at 18:24
Obviously this depends on your situation, but maybe it makes sense to just shorten/improve the column names? But for a workaround, you could consider converting to a "fake" multi-index. E.g. if column name is "david s pumpkins" you could have "david s" as the top level and "pumpkins" as the second level. Obviously that's not what multi-indexes are for, but I think it would basically do what you want.
– JohnE
Oct 13 '17 at 2:37
Obviously this depends on your situation, but maybe it makes sense to just shorten/improve the column names? But for a workaround, you could consider converting to a "fake" multi-index. E.g. if column name is "david s pumpkins" you could have "david s" as the top level and "pumpkins" as the second level. Obviously that's not what multi-indexes are for, but I think it would basically do what you want.
– JohnE
Oct 13 '17 at 2:37
@JohnE, Thanks for the suggestion. A text wrap is what @Wen had suggested which is deleted but was:
df.style.set_table_styles([dict(selector="th",props=[('max-width', '50px')])])
It's a good work-around that works for phrases, like in my example. I also have long words though. I'm still curious to see if rotation is possible! I'm no CSS selector expert, but I thought this should work but doesn't: dfoo.style.set_table_styles([dict(selector="th",props=[('text-orientation', 'upright')])])
– Dan Fiorino
Oct 13 '17 at 15:49
@JohnE, Thanks for the suggestion. A text wrap is what @Wen had suggested which is deleted but was:
df.style.set_table_styles([dict(selector="th",props=[('max-width', '50px')])])
It's a good work-around that works for phrases, like in my example. I also have long words though. I'm still curious to see if rotation is possible! I'm no CSS selector expert, but I thought this should work but doesn't: dfoo.style.set_table_styles([dict(selector="th",props=[('text-orientation', 'upright')])])
– Dan Fiorino
Oct 13 '17 at 15:49
I just re-read the doc page (pandas.pydata.org/pandas-docs/version/0.19.2/style.html) which states that 'You can only style the values, not the index or columns', but the doc shows columns and indices being formatted. Maybe this is is just starting to be addressed.
– Dan Fiorino
Oct 13 '17 at 16:18
I just re-read the doc page (pandas.pydata.org/pandas-docs/version/0.19.2/style.html) which states that 'You can only style the values, not the index or columns', but the doc shows columns and indices being formatted. Maybe this is is just starting to be addressed.
– Dan Fiorino
Oct 13 '17 at 16:18
add a comment |
4 Answers
4
active
oldest
votes
Something like:
data = [{'Way too long of a column to be reasonable':4,'Four?':4},
{'Way too long of a column to be reasonable':5,'Four?':5}]
dfoo = pd.DataFrame(data)
dfoo.style.set_table_styles(
[dict(selector="th",props=[('max-width', '80px')]),
dict(selector="th.col_heading",
props=[("writing-mode", "vertical-rl"),
('transform', 'rotateZ(-90deg)'),
])]
)
is probably close to what you want:
see result here
Changing my accepted answer. This does the task using only the Pandas library and will display in a JupyterLab. Well done!
– Dan Fiorino
Nov 15 '18 at 22:53
add a comment |
I can get it so that the text is completely turned around 90 degrees, but can't figure out how to use text-orientation: upright
as it just makes the text invisible :( You were missing the writing-mode
property that has to be set for text-orientation
to have any effect. Also, I made it only apply to column headings by modifying the selector a little.
dfoo.style.set_table_styles([dict(selector="th.col_heading",props=[("writing-mode", "vertical-lr"),('text-orientation', 'upright')])])
Hopefully this gets you a little closer to your goal!
This is definitely getting warmer! All of the individual letters are rotated at least
– Dan Fiorino
Oct 18 '17 at 1:14
add a comment |
Using the Python library 'pybloqs' (http://pybloqs.readthedocs.io/en/latest/), it is possible to rotate the column names as well as add a padding to the top. The only downside (as the documentation mentions) is that the top-padding does not work inline with Jupyter. The table must be exported.
import pandas as pd
from pybloqs import Block
import pybloqs.block.table_formatters as tf
from IPython.core.display import display, HTML
data = [{'Way too long of a column to be reasonable':4,'Four?':4},
{'Way too long of a column to be reasonable':5,'Four?':5}]
dfoo =pd.DataFrame(data)
fmt_header = tf.FmtHeader(fixed_width='5cm',index_width='10%',
top_padding='10cm', rotate_deg=60)
table_block = Block(dfoo, formatters=[fmt_header])
display(HTML(table_block.render_html()))
table_block.save('Table.html')
add a comment |
Looking at the pybloqs source code for the accepted answer's solution, I was able to find out how to rotate the columns without installing pybloqs. Note that this also rotates the index, but I have added code to remove those.
from IPython.display import HTML, display
data = [{'Way too long of a column to be reasonable':4,'Four?':4},
{'Way too long of a column to be reasonable':5,'Four?':5}]
df = pd.DataFrame(data)
styles = [
dict(selector="th", props=[("font-size", "125%"),
("text-align", "center"),
("transform", "translate(0%,-30%) rotate(-5deg)")
]),
dict(selector=".row_heading, .blank", props= [('display', 'none;')])
]
html = df.style.set_table_styles(styles).render()
display(HTML(html))
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f46715736%2frotating-the-column-name-for-a-panda-dataframe%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
Something like:
data = [{'Way too long of a column to be reasonable':4,'Four?':4},
{'Way too long of a column to be reasonable':5,'Four?':5}]
dfoo = pd.DataFrame(data)
dfoo.style.set_table_styles(
[dict(selector="th",props=[('max-width', '80px')]),
dict(selector="th.col_heading",
props=[("writing-mode", "vertical-rl"),
('transform', 'rotateZ(-90deg)'),
])]
)
is probably close to what you want:
see result here
Changing my accepted answer. This does the task using only the Pandas library and will display in a JupyterLab. Well done!
– Dan Fiorino
Nov 15 '18 at 22:53
add a comment |
Something like:
data = [{'Way too long of a column to be reasonable':4,'Four?':4},
{'Way too long of a column to be reasonable':5,'Four?':5}]
dfoo = pd.DataFrame(data)
dfoo.style.set_table_styles(
[dict(selector="th",props=[('max-width', '80px')]),
dict(selector="th.col_heading",
props=[("writing-mode", "vertical-rl"),
('transform', 'rotateZ(-90deg)'),
])]
)
is probably close to what you want:
see result here
Changing my accepted answer. This does the task using only the Pandas library and will display in a JupyterLab. Well done!
– Dan Fiorino
Nov 15 '18 at 22:53
add a comment |
Something like:
data = [{'Way too long of a column to be reasonable':4,'Four?':4},
{'Way too long of a column to be reasonable':5,'Four?':5}]
dfoo = pd.DataFrame(data)
dfoo.style.set_table_styles(
[dict(selector="th",props=[('max-width', '80px')]),
dict(selector="th.col_heading",
props=[("writing-mode", "vertical-rl"),
('transform', 'rotateZ(-90deg)'),
])]
)
is probably close to what you want:
see result here
Something like:
data = [{'Way too long of a column to be reasonable':4,'Four?':4},
{'Way too long of a column to be reasonable':5,'Four?':5}]
dfoo = pd.DataFrame(data)
dfoo.style.set_table_styles(
[dict(selector="th",props=[('max-width', '80px')]),
dict(selector="th.col_heading",
props=[("writing-mode", "vertical-rl"),
('transform', 'rotateZ(-90deg)'),
])]
)
is probably close to what you want:
see result here
edited Nov 15 '18 at 11:59
a_k_v
794112
794112
answered Nov 15 '18 at 11:39
BobainBobain
363
363
Changing my accepted answer. This does the task using only the Pandas library and will display in a JupyterLab. Well done!
– Dan Fiorino
Nov 15 '18 at 22:53
add a comment |
Changing my accepted answer. This does the task using only the Pandas library and will display in a JupyterLab. Well done!
– Dan Fiorino
Nov 15 '18 at 22:53
Changing my accepted answer. This does the task using only the Pandas library and will display in a JupyterLab. Well done!
– Dan Fiorino
Nov 15 '18 at 22:53
Changing my accepted answer. This does the task using only the Pandas library and will display in a JupyterLab. Well done!
– Dan Fiorino
Nov 15 '18 at 22:53
add a comment |
I can get it so that the text is completely turned around 90 degrees, but can't figure out how to use text-orientation: upright
as it just makes the text invisible :( You were missing the writing-mode
property that has to be set for text-orientation
to have any effect. Also, I made it only apply to column headings by modifying the selector a little.
dfoo.style.set_table_styles([dict(selector="th.col_heading",props=[("writing-mode", "vertical-lr"),('text-orientation', 'upright')])])
Hopefully this gets you a little closer to your goal!
This is definitely getting warmer! All of the individual letters are rotated at least
– Dan Fiorino
Oct 18 '17 at 1:14
add a comment |
I can get it so that the text is completely turned around 90 degrees, but can't figure out how to use text-orientation: upright
as it just makes the text invisible :( You were missing the writing-mode
property that has to be set for text-orientation
to have any effect. Also, I made it only apply to column headings by modifying the selector a little.
dfoo.style.set_table_styles([dict(selector="th.col_heading",props=[("writing-mode", "vertical-lr"),('text-orientation', 'upright')])])
Hopefully this gets you a little closer to your goal!
This is definitely getting warmer! All of the individual letters are rotated at least
– Dan Fiorino
Oct 18 '17 at 1:14
add a comment |
I can get it so that the text is completely turned around 90 degrees, but can't figure out how to use text-orientation: upright
as it just makes the text invisible :( You were missing the writing-mode
property that has to be set for text-orientation
to have any effect. Also, I made it only apply to column headings by modifying the selector a little.
dfoo.style.set_table_styles([dict(selector="th.col_heading",props=[("writing-mode", "vertical-lr"),('text-orientation', 'upright')])])
Hopefully this gets you a little closer to your goal!
I can get it so that the text is completely turned around 90 degrees, but can't figure out how to use text-orientation: upright
as it just makes the text invisible :( You were missing the writing-mode
property that has to be set for text-orientation
to have any effect. Also, I made it only apply to column headings by modifying the selector a little.
dfoo.style.set_table_styles([dict(selector="th.col_heading",props=[("writing-mode", "vertical-lr"),('text-orientation', 'upright')])])
Hopefully this gets you a little closer to your goal!
answered Oct 16 '17 at 15:56
Louise DaviesLouise Davies
4,76122029
4,76122029
This is definitely getting warmer! All of the individual letters are rotated at least
– Dan Fiorino
Oct 18 '17 at 1:14
add a comment |
This is definitely getting warmer! All of the individual letters are rotated at least
– Dan Fiorino
Oct 18 '17 at 1:14
This is definitely getting warmer! All of the individual letters are rotated at least
– Dan Fiorino
Oct 18 '17 at 1:14
This is definitely getting warmer! All of the individual letters are rotated at least
– Dan Fiorino
Oct 18 '17 at 1:14
add a comment |
Using the Python library 'pybloqs' (http://pybloqs.readthedocs.io/en/latest/), it is possible to rotate the column names as well as add a padding to the top. The only downside (as the documentation mentions) is that the top-padding does not work inline with Jupyter. The table must be exported.
import pandas as pd
from pybloqs import Block
import pybloqs.block.table_formatters as tf
from IPython.core.display import display, HTML
data = [{'Way too long of a column to be reasonable':4,'Four?':4},
{'Way too long of a column to be reasonable':5,'Four?':5}]
dfoo =pd.DataFrame(data)
fmt_header = tf.FmtHeader(fixed_width='5cm',index_width='10%',
top_padding='10cm', rotate_deg=60)
table_block = Block(dfoo, formatters=[fmt_header])
display(HTML(table_block.render_html()))
table_block.save('Table.html')
add a comment |
Using the Python library 'pybloqs' (http://pybloqs.readthedocs.io/en/latest/), it is possible to rotate the column names as well as add a padding to the top. The only downside (as the documentation mentions) is that the top-padding does not work inline with Jupyter. The table must be exported.
import pandas as pd
from pybloqs import Block
import pybloqs.block.table_formatters as tf
from IPython.core.display import display, HTML
data = [{'Way too long of a column to be reasonable':4,'Four?':4},
{'Way too long of a column to be reasonable':5,'Four?':5}]
dfoo =pd.DataFrame(data)
fmt_header = tf.FmtHeader(fixed_width='5cm',index_width='10%',
top_padding='10cm', rotate_deg=60)
table_block = Block(dfoo, formatters=[fmt_header])
display(HTML(table_block.render_html()))
table_block.save('Table.html')
add a comment |
Using the Python library 'pybloqs' (http://pybloqs.readthedocs.io/en/latest/), it is possible to rotate the column names as well as add a padding to the top. The only downside (as the documentation mentions) is that the top-padding does not work inline with Jupyter. The table must be exported.
import pandas as pd
from pybloqs import Block
import pybloqs.block.table_formatters as tf
from IPython.core.display import display, HTML
data = [{'Way too long of a column to be reasonable':4,'Four?':4},
{'Way too long of a column to be reasonable':5,'Four?':5}]
dfoo =pd.DataFrame(data)
fmt_header = tf.FmtHeader(fixed_width='5cm',index_width='10%',
top_padding='10cm', rotate_deg=60)
table_block = Block(dfoo, formatters=[fmt_header])
display(HTML(table_block.render_html()))
table_block.save('Table.html')
Using the Python library 'pybloqs' (http://pybloqs.readthedocs.io/en/latest/), it is possible to rotate the column names as well as add a padding to the top. The only downside (as the documentation mentions) is that the top-padding does not work inline with Jupyter. The table must be exported.
import pandas as pd
from pybloqs import Block
import pybloqs.block.table_formatters as tf
from IPython.core.display import display, HTML
data = [{'Way too long of a column to be reasonable':4,'Four?':4},
{'Way too long of a column to be reasonable':5,'Four?':5}]
dfoo =pd.DataFrame(data)
fmt_header = tf.FmtHeader(fixed_width='5cm',index_width='10%',
top_padding='10cm', rotate_deg=60)
table_block = Block(dfoo, formatters=[fmt_header])
display(HTML(table_block.render_html()))
table_block.save('Table.html')
edited Oct 18 '17 at 14:26
answered Oct 18 '17 at 1:32
Dan FiorinoDan Fiorino
9511
9511
add a comment |
add a comment |
Looking at the pybloqs source code for the accepted answer's solution, I was able to find out how to rotate the columns without installing pybloqs. Note that this also rotates the index, but I have added code to remove those.
from IPython.display import HTML, display
data = [{'Way too long of a column to be reasonable':4,'Four?':4},
{'Way too long of a column to be reasonable':5,'Four?':5}]
df = pd.DataFrame(data)
styles = [
dict(selector="th", props=[("font-size", "125%"),
("text-align", "center"),
("transform", "translate(0%,-30%) rotate(-5deg)")
]),
dict(selector=".row_heading, .blank", props= [('display', 'none;')])
]
html = df.style.set_table_styles(styles).render()
display(HTML(html))
add a comment |
Looking at the pybloqs source code for the accepted answer's solution, I was able to find out how to rotate the columns without installing pybloqs. Note that this also rotates the index, but I have added code to remove those.
from IPython.display import HTML, display
data = [{'Way too long of a column to be reasonable':4,'Four?':4},
{'Way too long of a column to be reasonable':5,'Four?':5}]
df = pd.DataFrame(data)
styles = [
dict(selector="th", props=[("font-size", "125%"),
("text-align", "center"),
("transform", "translate(0%,-30%) rotate(-5deg)")
]),
dict(selector=".row_heading, .blank", props= [('display', 'none;')])
]
html = df.style.set_table_styles(styles).render()
display(HTML(html))
add a comment |
Looking at the pybloqs source code for the accepted answer's solution, I was able to find out how to rotate the columns without installing pybloqs. Note that this also rotates the index, but I have added code to remove those.
from IPython.display import HTML, display
data = [{'Way too long of a column to be reasonable':4,'Four?':4},
{'Way too long of a column to be reasonable':5,'Four?':5}]
df = pd.DataFrame(data)
styles = [
dict(selector="th", props=[("font-size", "125%"),
("text-align", "center"),
("transform", "translate(0%,-30%) rotate(-5deg)")
]),
dict(selector=".row_heading, .blank", props= [('display', 'none;')])
]
html = df.style.set_table_styles(styles).render()
display(HTML(html))
Looking at the pybloqs source code for the accepted answer's solution, I was able to find out how to rotate the columns without installing pybloqs. Note that this also rotates the index, but I have added code to remove those.
from IPython.display import HTML, display
data = [{'Way too long of a column to be reasonable':4,'Four?':4},
{'Way too long of a column to be reasonable':5,'Four?':5}]
df = pd.DataFrame(data)
styles = [
dict(selector="th", props=[("font-size", "125%"),
("text-align", "center"),
("transform", "translate(0%,-30%) rotate(-5deg)")
]),
dict(selector=".row_heading, .blank", props= [('display', 'none;')])
]
html = df.style.set_table_styles(styles).render()
display(HTML(html))
answered Sep 5 '18 at 14:24
scottlittlescottlittle
5,62532546
5,62532546
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f46715736%2frotating-the-column-name-for-a-panda-dataframe%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
1
could you please added tag
jupyter notebook
– Wen-Ben
Oct 12 '17 at 18:24
Obviously this depends on your situation, but maybe it makes sense to just shorten/improve the column names? But for a workaround, you could consider converting to a "fake" multi-index. E.g. if column name is "david s pumpkins" you could have "david s" as the top level and "pumpkins" as the second level. Obviously that's not what multi-indexes are for, but I think it would basically do what you want.
– JohnE
Oct 13 '17 at 2:37
@JohnE, Thanks for the suggestion. A text wrap is what @Wen had suggested which is deleted but was:
df.style.set_table_styles([dict(selector="th",props=[('max-width', '50px')])])
It's a good work-around that works for phrases, like in my example. I also have long words though. I'm still curious to see if rotation is possible! I'm no CSS selector expert, but I thought this should work but doesn't:dfoo.style.set_table_styles([dict(selector="th",props=[('text-orientation', 'upright')])])
– Dan Fiorino
Oct 13 '17 at 15:49
I just re-read the doc page (pandas.pydata.org/pandas-docs/version/0.19.2/style.html) which states that 'You can only style the values, not the index or columns', but the doc shows columns and indices being formatted. Maybe this is is just starting to be addressed.
– Dan Fiorino
Oct 13 '17 at 16:18