How to Print a Pandas DataFrame Without Index and Format Datetime Columns?

Susan Sarandon
Release: 2024-11-04 18:47:02
Original
226 people have browsed it

How to Print a Pandas DataFrame Without Index and Format Datetime Columns?

Printing Pandas DataFrame without Index and Formatting Datetime

When working with Pandas dataframes, it may be necessary to print the dataframe without the index. This article will demonstrate how to print a dataframe without an index while also formatting a datetime column to display only the time.

Consider the following dataframe with a datetime column:

   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 print the dataframe without the index, use the to_string() method with the index parameter set to False:

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

This will 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

Additionally, to format the datetime column to display only the time, use the dt.time accessor:

<code class="python">df['Enter Time'] = df['Enter Time'].dt.time</code>
Copy after login

This will modify the dataframe in-place, and the datetime column will now display only the time:

   User ID   Enter Time   Activity Number
0      123   00:09:00              1411
1      123   00:18:00               893
2      123   00:49:00              1041
Copy after login

Now, printing the dataframe without the index using the to_string() method 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 Index and Format Datetime Columns?. 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
Latest Articles by Author
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!