Home >Backend Development >Python Tutorial >How to Print a Pandas DataFrame Without the Index?

How to Print a Pandas DataFrame Without the Index?

DDD
DDDOriginal
2024-11-06 06:47:021102browse

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn