1

I have this code:

from tabulate import tabulate                                                                                                                                           
import pandas                                                                                                                                                     

df = pandas.DataFrame({'Col2' : ['Hello', 'How' , 'Are', 'You'],                                                                                                            
                   'Col3' : ['Hi', 'I', 'am', 'fine']})                                                                                                                 
nice_table = tabulate(df, headers='keys', tablefmt='psql')                                                                                                              
print(nice_table)   

It prints this:

+----+--------+--------+                                                                                                                                                
|    | Col2   | Col3   |                                                                                                                                                
|----+--------+--------|                                                                                                                                                
|  0 | Hello  | Hi     |                                                                                                                                                
|  1 | How    | I      |                                                                                                                                                
|  2 | Are    | am     |                                                                                                                                                
|  3 | You    | fine   |                                                                                                                                                
+----+--------+--------+  

Is there a way to access and print the content of a given cell of nice_table?

2 Answers2

1

No. Keep in mind that tabulate's sole purpose is as mentioned in the documentation is to:

Pretty-print tabular data in Python

Moreover if you run type(nice_table) you'll see that tabulate returns a string. Therefore for any operations other than pretty printing the dataframe you will have to work with df.

yatu
  • 86,083
  • 12
  • 84
  • 139
-1
df.loc[[2, 'Col3']]
#am
df.loc[[0, 'Col2']]
#Hello

for more information: pandas.DataFrame.loc

ycx
  • 3,155
  • 3
  • 14
  • 26
  • I am getting: `KeyError: "None of [['2', 'Col3']] are in the [index]" ` –  Dec 13 '18 at 12:44
  • Apologies, I had thought you set the index to strings. I've editted the answer to reflect the correct one. `df.loc[[2, 'Col3']]` and `df.loc[[0, 'Col2']]` – ycx Dec 13 '18 at 13:01