How to print Dataframe in Python without Index?
Prerequisites: Pandas
When printing a dataframe, by default, index appears with the output but this can be removed if required. To print the dataframe without indices index parameter in to_string() must be set to False.
Syntax:
df.to_string(index = False)
Approach:
- Import module
- Create a simple dataframe .
- Set index to false in to_string()
- Print result
Implementation using this approach is given below
Example 1:
import pandas as pd df = pd.DataFrame({ "Name" : [ "sachin" , "sujay" , "Amara" , "shivam" , "Manoj" ], "Stream" : [ "Humanities" , "Science" , "Science" , "Commerce" , "Humanities" ]}, index = [ "A" , "B" , "C" , "D" , "E" ]) print ( "THIS IS THE ORIGINAL DATAFRAME:" ) display(df) print () print ( "THIS IS THE DATAFRAME WITHOUT INDEX VAL" ) print (df.to_string(index = False )) |
Output:
Example 2:
- Python3
import pandas as pd df = pd.DataFrame([ "Geeks" , "for" , "geek" ], index = [ "A" , "B" , "C" ]) print ( "THIS IS THE ORIGINAL DATAFRAME:" ) display(df) print () print ( "THIS IS THE DATAFRAME WITHOUT INDEX VAL" ) print (df.to_string(index = False )) |
Output:
Last Updated on October 24, 2021 by admin