How to Print (To Paper) a Nicely-Formatted Data Frame

How to print (to paper) a nicely-formatted data frame

Here is a quick and easy possibility using grid.table from the gridExtra package:

library(gridExtra)
pdf("data_output.pdf", height=11, width=8.5)
grid.table(mtcars)
dev.off()

Sample Image

If your data doesn't fit on the page, you can reduce the text size grid.table(mtcars, gp=gpar(fontsize=8)). This may not be very flexible, nor easy to generalize or automate.

How do I turn this oddly formatted looped print function into a data frame with similar output?

Create nested lists and convert to DataFrame:

L = []
for sent in nltk.sent_tokenize(sentence):
for chunk in nltk.ne_chunk(nltk.pos_tag(nltk.word_tokenize(sent))):
if hasattr(chunk, 'label'):
L.append([chunk.label(), ' '.join(c[0] for c in chunk)])

df = pd.DataFrame(L, columns=['a','b'])
print (df)
a b
0 PERSON Martin
1 PERSON Luther King
2 PERSON Michael King
3 ORGANIZATION American
4 GPE American
5 GPE Christian
6 PERSON Mahatma Gandhi
7 PERSON Martin Luther

In list comperehension solution is:

L= [[chunk.label(), ' '.join(c[0] for c in chunk)]  
for sent in nltk.sent_tokenize(sentence)
for chunk in nltk.ne_chunk(nltk.pos_tag(nltk.word_tokenize(sent)))
if hasattr(chunk, 'label')]

df = pd.DataFrame(L, columns=['a','b'])

How to print a data.frame in a format that can be reused

The function you're looking for is dput:

> a = data.frame(A=c(1:6),B=c(1:6),C=c(1:6))
> dput(a)
structure(list(A = 1:6, B = 1:6, C = 1:6), .Names = c("A", "B",
"C"), row.names = c(NA, -6L), class = "data.frame")
> b = dput(a)
> b
A B C
1 1 1 1
2 2 2 2
3 3 3 3
4 4 4 4
5 5 5 5
6 6 6 6

Pretty-print an entire Pandas Series / DataFrame

You can also use the option_context, with one or more options:

with pd.option_context('display.max_rows', None, 'display.max_columns', None):  # more options can be specified also
print(df)

This will automatically return the options to their previous values.

If you are working on jupyter-notebook, using display(df) instead of print(df) will use jupyter rich display logic (like so).

Print a data frame with columns aligned (as displayed in R)

You could redirect the output of print to file.

max.print <- getOption('max.print')
options(max.print=nrow(dframe) * ncol(dframe))
sink('dframe.txt')
dframe
sink()
options(max.print=max.print)

Why does jupyter sometimes print a DataFrame formatted and sometimes as text?

Why does jupyter sometimes print a DataFrame formatted and sometimes as text?

if you use the function - print it will print it as text because print is using the function to_string for any object it gets.

and when you "leave" the data frame at the and of the cell it will show it as a table because it one of the functions that Jupiter does.

Sample Image

the function test_csv.drop(columns=['platform']) returns df
if you want it to do the drop in the dataFrame you have to use inplace=True or df=df.drop(col)...
and than print the dataFrame

Windows: Print to printer from R - quick and dirty is OK

Here's a bit of code that I use all the time:

print_to_printer <-function(df, addquotes = FALSE){     
write.csv(df, "c:\\PRINT_ME.txt", quote = addquotes)
shell("NOTEPAD /P c:\\PRINT_ME.txt")
cat(quote(df), " has been printed to printer")}

then you can use it with

print_to_printer(my_df) 

if your data.frame is called my_df. Your can change the printed quotes on or off, as needed.

I have to give credit to Alin Constantin for helping me to derive the solution in R: http://alinconstantin.blogspot.com/2012/08/notepadexe-command-line-arguments.html



Related Topics



Leave a reply



Submit