Home  >  Article  >  Backend Development  >  How to Print a Pandas DataFrame Without Index and Format Datetime Columns?

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

Susan Sarandon
Susan SarandonOriginal
2024-11-04 18:47:02227browse

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

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>

This will output:

User ID   Enter Time   Activity Number
123         00:09:00              1411
123         00:18:00               893
123         00:49:00              1041

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>

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

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

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!

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