I would like to print a data frame where the columns are center aligned. Below is what I have I tried, I thought printing the data frame test1 would result in the columns being aligned in the center but this is not the case. Any thoughts on how I can do this?
test=data.frame(x=c(1,2,3),y=c(5,6,7))
names(test)=c('Variable 1','Variable 2')
test[,1]=as.character(test[,1])
test[,2]=as.character(test[,2])
test1=format(test,justify='centre')
print(test,row.names=FALSE,quote=FALSE)
Variable 1 Variable 2
1 5
2 6
3 7
print(test1,row.names=FALSE,quote=FALSE)
Variable 1 Variable 2
1 5
2 6
3 7
The problem is that in order for this to work as you expect, the "width
" argument needs to also be specified.
Here's an example:
test.1 <- data.frame(Variable.1 = as.character(c(1,2,3)),
Variable.2 = as.character(c(5,6,7)))
# Identify the width of the widest column by column name
name.width <- max(sapply(names(test.1), nchar))
format(test.1, width = name.width, justify = "centre")
# Variable.1 Variable.2
# 1 1 5
# 2 2 6
# 3 3 7
But, how does this approach work with columns where the variable names are different lengths? Not so well.
test.2 <- data.frame(A.Really.Long.Variable.Name = as.character(c(1,2,3)),
Short.Name = as.character(c(5,6,7)))
name.width <- max(sapply(names(test.2), nchar))
format(test.2, width = name.width, justify = "centre")
# A.Really.Long.Variable.Name Short.Name
# 1 1 5
# 2 2 6
# 3 3 7
There is, of course, a workaround: change the "width" of each variable name to be equal lengths by padding them with spaces (using format()
)
orig.names <- names(test.2) # in case you want to restore the original names
names(test.2) <- format(names(test.2), width = name.width, justify = "centre")
format(test.2, width = name.width, justify = "centre")
# A.Really.Long.Variable.Name Short.Name
# 1 1 5
# 2 2 6
# 3 3 7
call this function to get a dataframe to display like that:
def pd_centered(df):
return df.style.set_table_styles([
{"selector": "th", "props": [("text-align", "center")]},
{"selector": "td", "props": [("text-align", "center")]}])
e.g.:
display(pd_centered(original_df))
This will center align both the headers and the actual data cells, and you may remove either the td
or the th
to disable either one if you like.
Source: https://github.com/pandas-dev/pandas/issues/12144
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With