How to Print a Pandas DataFrame Without the Index?

DDD
Release: 2024-11-06 06:47:02
Original
977 people have browsed it

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
Copy after login

To remove the index column, use the following code:

<code class="python">print(df.to_string(index=False))</code>
Copy after login

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
Copy after login
Copy after login

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>
Copy after login

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
Copy after login
Copy after login

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!

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!