Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> Python

Cách hiển thị Pandas DataFrame trong Python không có Index?

Dùng index=False để bỏ qua index.

Nhập thư viện:

import pandas as pd

Tạo DataFrame:

dataFrame = pd.DataFrame([[10, 15], [20, 25], [30, 35]],
                         index=['x', 'y', 'z'],
                         columns=['a', 'b'])

Chọn hàng bằng loc:

dataFrame.loc['x']

Hiển thị DataFrame không index:

dataFrame.to_string(index=False)

Ví dụ

import pandas as pd

dataFrame = pd.DataFrame([[10, 15], [20, 25], [30, 35]],
                         index=['x', 'y', 'z'],
                         columns=['a', 'b'])

print("Displaying DataFrame with index...\n", dataFrame)
print("\nSelect rows by passing label...")
print(dataFrame.loc['x'])
print("\nDisplaying DataFrame without Index...\n", dataFrame.to_string(index=False))

Đầu ra

Displaying DataFrame with index...
    a   b
x  10  15
y  20  25
z  30  35

Select rows by passing label...
a    10
b    15
Name: x, dtype: int64

Displaying DataFrame without Index...
   a   b
  10  15
  20  25
  30  35