To print Pandas DataFrame without an index, you need to convert DataFrame into a String using the df.to_string() method and pass the “index=False” argument. Here, df means DataFrame.
The “index=False” argument specifies that the row index should not be included in the output.
Printing with index
import pandas as pd data = {'Column1': [1, 2, 3], 'Column2': [4, 5, 6]} df = pd.DataFrame(data) print(df)
Output
Printing without index
Instead of just writing print(df), we can use print(df.to_string(index=False)) syntax.
import pandas as pd data = {'Column1': [1, 2, 3], 'Column2': [4, 5, 6]} df = pd.DataFrame(data) print(df.to_string(index=False))
Output
You can see that only column names and values have been printed from the above output image.
Using the style.hide_index() Method
The df.style.hide_index() method returns a styled representation of a DataFrame without the index column.
Please ensure that you run the below code in an Interactive environment like JupyterLab or online notebooks.
import pandas as pd data = {'Column1': [1, 2, 3], 'Column2': [4, 5, 6]} df = pd.DataFrame(data) # Create a styled representation of the DataFrame without index styled_df = df.style.hide() # Print the styled DataFrame styled_df
Output
You can see that the above DataFrame is without an index. Here, we don’t have to convert it into a string.
Why do we need to print without row index?
- It will help us read the better data without so much “clutter”.
- Omitting indices can save space and reduce file sizes.
- By controlling the display, you can tailor the output to our specific needs.