Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python

Python Pandas – ดู Data Type และข้อมูลคอลัมน์ของ DataFrame (info)

ภาพรวม

ใช้ DataFrame.info() แสดงข้อมูลครบ: index, columns, non-null count, dtype, memory usage

ตัวอย่างโค้ด


import pandas as pd

dataFrame = pd.DataFrame({
    "Car": ['BMW', 'Audi', 'BMW', 'Lexus', 'Tesla', 'Lexus', 'Mustang'],
    "Place": ['Delhi', 'Bangalore', 'Hyderabad', 'Chandigarh', 'Pune', 'Mumbai', 'Jaipur'],
    "Units": [100, 150, 50, 110, 90, 120, 80]
})

print("DataFrame ...\n", dataFrame)
print("\nUnique values from a column ...\n", dataFrame['Car'].unique())
print("\nCount unique values from a column ...\n", dataFrame['Car'].nunique())
print("\nGet the datatype info ...\n")
dataFrame.info()

ผลลัพธ์

DataFrame ...
       Car       Place  Units
0      BMW     Delhi    100
1    Audi  Bangalore    150
2      BMW  Hyderabad     50
3   Lexus Chandigarh    110
4   Tesla      Pune     90
5   Lexus    Mumbai    120
6  Mustang    Jaipur     80

Unique values from a column ...
['BMW' 'Audi' 'Lexus' 'Tesla' 'Mustang']

Count unique values from a column ...
5

Get the datatype info ...
RangeIndex: 7 entries, 0 to 6
Data columns (total 3 columns):
 Car      7 non-null object
 Place    7 non-null object
 Units    7 non-null int64
dtypes: int64(1), object(2)
memory usage: 240.0+ bytes

คำอธิบาย Output ของ info()

  • RangeIndex: ประเภทและช่วงของ index
  • Data columns: จำนวนคอลัมน์และรายละเอียดแต่ละคอลัมน์
  • non-null: จำนวนค่าที่ไม่ใช่ NaN
  • dtype: ชนิดข้อมูล (object=string, int64, float64, bool, datetime64, category)
  • dtypes summary: นับจำนวนคอลัมน์แต่ละ dtype
  • memory usage: หน่วยความจำประมาณ (deep=True สำหรับค่าจริง)

Parameters ที่มีประโยชน์


df.info(verbose=True)      # แสดงทุกคอลัมน์ (default)
df.info(verbose=False)     # แสดง summary เฉพาะ
df.info(memory_usage='deep') # คำนวณ memory จริง (รวม object contents)
df.info(null_counts=True)  # แสดง null count (default True)

คำสั่งเกี่ยวข้อง

  • df.dtypes – Series ของ dtype ทุกคอลัมน์
  • df.describe() – สถิติพื้นฐาน (count, mean, std, min, max, quartiles)
  • df.shape – (rows, cols)
  • df.memory_usage(deep=True) – memory ต่อคอลัมน์