Home > Article > Backend Development > How to Print a Pandas DataFrame Without the Index?
Printing a Pandas DataFrame without Index
To print a Pandas DataFrame without its index, you can use the to_string method with the index=False parameter. This will hide the index column from the output.
Consider the following DataFrame:
User ID Enter Time Activity Number 0 123 2014-07-08 00:09:00 1411 1 123 2014-07-08 00:18:00 893 2 123 2014-07-08 00:49:00 1041
To remove the index column, use the following code:
<code class="python">print(df.to_string(index=False))</code>
This will print the DataFrame as:
User ID Enter Time Activity Number 123 00:09:00 1411 123 00:18:00 893 123 00:49:00 1041
To further refine the output, consider that the Enter Time column is a datetime type. You can use the dt.time accessor to extract only the time component from the column before printing the DataFrame.
<code class="python">print(df[['User ID', df['Enter Time'].dt.time, 'Activity Number']].to_string(index=False))</code>
This will produce the desired output:
User ID Enter Time Activity Number 123 00:09:00 1411 123 00:18:00 893 123 00:49:00 1041
The above is the detailed content of How to Print a Pandas DataFrame Without the Index?. For more information, please follow other related articles on the PHP Chinese website!