5

I have tried using display method from IPython I want to print with no indexes and center align the values in all columns. I am not using Jupyter notebook; I'm using pyCharm IDE.

import pandas as pd
from IPython.core.display import display


data = {'Name':['Tom', 'nick', 'krish', 'jack'], 
         'Age':[20, 21, 19, 18]}

df = pd.DataFrame(data)
df = df.style.set_properties(**{'text-align': 'center'}).hide_index()
display(df)

The output I got is this (screenshot):

**<pandas.io.formats.style.Styler object at 0x0000029ED0913550>**

I want to do it in .py file. Also is there any way I can print dataframes of pandas in Sciview or Database tool windows that are available in pyCharm? SciView

smci
  • 32,567
  • 20
  • 113
  • 146
abhinit21
  • 330
  • 1
  • 4
  • 13
  • 3
    You cannot use the Styler or `display` outside of a dynamic HTML environment like Jupyter notebook/IPython. – Henry Ecker Aug 07 '21 at 18:19
  • Is there any other way to print without indexes and center aligned? or any one of those – abhinit21 Aug 07 '21 at 18:21
  • 2
    Something like `print(df.to_string(index=False, justify='center'))` or with `tabulate` as `print(df.to_markdown(index=False, tablefmt='plain', colalign=df.columns.map(lambda _: 'center')))` – Henry Ecker Aug 07 '21 at 18:24
  • 2
    You can also use `df.to_html` or `df.to_excel` not only Jupyter notebook. – Corralien Aug 07 '21 at 19:14

1 Answers1

0

I figured it out. I needed to change the bottom portion of my code (when I export the data to excel) to the following:

# Append the difference to an existing Excel File
with pd.ExcelWriter('OldData.xlsx', mode='a', engine='openpyxl', if_sheet_exists='replace') as writer:
    difference.to_excel(writer, index=False, sheet_name="1-25-2023")
    print(difference)
  • This answer is misleading: the OP's question was not about exporting `to_excel()`, it was about displaying the dataframe inside pyCharm IDE windows (not jupyter). Your use-cases are different! – smci Aug 24 '23 at 07:35