developer tip

인덱스없이 팬더 DataFrame을 인쇄하는 방법

optionbox 2020. 7. 27. 07:52
반응형

인덱스없이 팬더 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

나는 그것을 인쇄하고 싶다

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

print df.to_string(index=False)

print(df.to_csv(sep='\t', index=False))

또는 가능하면 :

print(df.to_csv(columns=['A', 'B', 'C'], sep='\t', index=False))

문자열 / JSON을 인쇄하려면 다음을 사용하여 해결할 수 있습니다.

print(df.to_string(index=False))

그러나 데이터를 직렬화하거나 MongoDB로 보내려면 다음과 같은 작업을 수행하는 것이 좋습니다.

document = df.to_dict(orient='list')

이제 데이터 방향을 지정하는 6 가지 방법이 있습니다. 팬더 문서 를 더 잘 확인하십시오.


데이터 프레임을 예쁘게 인쇄하려면 tabulate package를 사용할 수 있습니다 .

import pandas as pd
import numpy as np
from tabulate import tabulate

def pprint_df(dframe):
    print tabulate(dframe, headers='keys', tablefmt='psql', showindex=False)

df = pd.DataFrame({'col1': np.random.randint(0, 100, 10), 
    'col2': np.random.randint(50, 100, 10), 
    'col3': np.random.randint(10, 10000, 10)})

pprint_df(df)

특히 showindex=False이름에서 알 수 있듯이을 사용하면 색인을 표시 할 수 없습니다. 결과는 다음과 같습니다.

+--------+--------+--------+
|   col1 |   col2 |   col3 |
|--------+--------+--------|
|     15 |     76 |   5175 |
|     30 |     97 |   3331 |
|     34 |     56 |   3513 |
|     50 |     65 |    203 |
|     84 |     75 |   7559 |
|     41 |     82 |    939 |
|     78 |     59 |   4971 |
|     98 |     99 |    167 |
|     81 |     99 |   6527 |
|     17 |     94 |   4267 |
+--------+--------+--------+

아래 줄은 인쇄 할 때 DataFrame의 색인 열을 숨 깁니다.

df.style.hide_index()

To answer the "How to print dataframe without an index" question, you can set the index to be an array of empty strings (one for each row in the dataframe), like this:

blankIndex=[''] * len(df)
df.index=blankIndex

If we use the data from your post:

row1 = (123, '2014-07-08 00:09:00', 1411)
row2 = (123, '2014-07-08 00:49:00', 1041)
row3 = (123, '2014-07-08 00:09:00', 1411)
data = [row1, row2, row3]
#set up dataframe
df = pd.DataFrame(data, columns=('User ID', 'Enter Time', 'Activity Number'))
print(df)

which would normally print out as:

   User ID           Enter Time  Activity Number
0      123  2014-07-08 00:09:00             1411
1      123  2014-07-08 00:49:00             1041
2      123  2014-07-08 00:09:00             1411

By creating an array with as many empty strings as there are rows in the data frame:

blankIndex=[''] * len(df)
df.index=blankIndex
print(df)

It will remove the index from the output:

  User ID           Enter Time  Activity Number
      123  2014-07-08 00:09:00             1411
      123  2014-07-08 00:49:00             1041
      123  2014-07-08 00:09:00             1411

And in Jupyter Notebooks would render as per this screenshot: Juptyer Notebooks dataframe with no index column


Similar to many of the answers above that use df.to_string(index=False), I often find it necessary to extract a single column of values in which case you can specify an individual column with .to_string using the following:

data = pd.DataFrame({'col1': np.random.randint(0, 100, 10), 
    'col2': np.random.randint(50, 100, 10), 
    'col3': np.random.randint(10, 10000, 10)})

print(data.to_string(columns=['col1'], index=False)

print(data.to_string(columns=['col1', 'col2'], index=False))

Which provides an easy to copy (and index free) output for use pasting elsewhere (Excel). Sample output:

col1  col2    
49    62    
97    97    
87    94    
85    61    
18    55

참고URL : https://stackoverflow.com/questions/24644656/how-to-print-pandas-dataframe-without-index

반응형