Lỗi thuộc tính trong gấu trúc Python

Pandas là gói python cho phép bạn tạo khung dữ liệu và thao tác với nó bằng nhiều chức năng khác nhau. Nhưng đôi khi bạn có thể gặp lỗi như AttributeError. Nếu bạn đang gặp lỗi mô-đun 'gấu trúc' không có thuộc tính 'bảng điều khiển' thì bài đăng này là dành cho bạn. Trong hướng dẫn này, bạn sẽ học cách giải quyết vấn đề về lỗi thuộc tính này bằng các phương pháp khác nhau

Bảng điều khiển trong Pandas là gì?

Panel là một chức năng cho phép bạn biểu diễn dữ liệu trong mảng ba chiều. Nó có dữ liệu, chỉ mục và cột. Nó cho phép bạn tạo dữ liệu đa chỉ mục và sử dụng nó, bạn có thể truy xuất thông tin từ tập dữ liệu một cách dễ dàng

Tại sao AttributeError. mô-đun 'gấu trúc' không có thuộc tính 'bảng điều khiển' Xảy ra

Nguyên nhân cốt lõi khiến mô-đun 'gấu trúc' này không có thuộc tính 'bảng điều khiển' là chức năng Panel[] không được hỗ trợ bởi thư viện Pandas hiện tại. Chức năng này không còn được hỗ trợ bởi phiên bản gấu trúc lớn hơn 0. 25. 0  phiên bản

Lý do khác có thể là gói pandas phải bị hỏng. Vậy là bạn đã cài đặt lại module pandas

Mục lục Ẩn

Chúng tôi nhận được AttributeError. mô-đun 'gấu trúc' không có thuộc tính 'khung dữ liệu' khi mô-đun Pandas không thể giải quyết và khởi tạo lớp DataFrame. AttributeError thường xảy ra nếu lớp không được định nghĩa trong mô-đun hoặc nếu nó có một tên khác

Trong bài viết này, chúng ta hãy xem AttributeError là gì. mô-đun 'gấu trúc' không có lỗi thuộc tính 'khung dữ liệu' có nghĩa là gì và cách giải quyết vấn đề này bằng các ví dụ

Lỗi thuộc tính. mô-đun 'gấu trúc' không có thuộc tính 'khung dữ liệu' Giải pháp

Có bốn lý do chính đằng sau lỗi này.  

  1. Bỏ qua trường hợp trong khi tạo DataFrame
  2. Khai báo tên module như một tên biến
  3. Đặt tên tệp là pd. py hoặc gấu trúc. py
  4. Mô-đun Pandas chưa được cài đặt

Lý do 1 – Bỏ qua trường hợp tạo DataFrame

Tên lớp DataFrame phân biệt chữ hoa chữ thường và được thể hiện bằng chữ hoa, chữ thường, nếu bạn đang sử dụng

raceback [most recent call last]:
  File "c:\Personal\IJS\Code\main.py", line 7, in 
    df = pd.dataframe[{'team': ['India', 'South Africa', 'New Zealand', 'England'],
    raise AttributeError[f"module 'pandas' has no attribute '{name}'"]

AttributeError: module 'pandas' has no attribute 'dataframe'. Did you mean: 'DataFrame'?
4 tất cả đều ở dạng chữ thường thì bạn sẽ nhận được mô-đun 'gấu trúc' không có thuộc tính 'khung dữ liệu' như minh họa bên dưới

# import pandas library
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt

# create pandas DataFrame
df = pd.dataframe[{'team': ['India', 'South Africa', 'New Zealand', 'England'],
                   'points': [10, 8, 3, 5],
                   'runrate': [0.5, 1.4, 2, -0.6],
                   'wins': [5, 4, 2, 2]}]

print[df]

đầu ra

raceback [most recent call last]:
  File "c:\Personal\IJS\Code\main.py", line 7, in 
    df = pd.dataframe[{'team': ['India', 'South Africa', 'New Zealand', 'England'],
    raise AttributeError[f"module 'pandas' has no attribute '{name}'"]

AttributeError: module 'pandas' has no attribute 'dataframe'. Did you mean: 'DataFrame'?

Chúng tôi có thể khắc phục sự cố bằng cách thay đổi khung dữ liệu thành DataFrame[chữ thường thành chữ thường] để tạo Khung dữ liệu Pandas. Khi chúng tôi chạy chương trình bây giờ, chúng tôi không gặp bất kỳ lỗi nào và mã chạy thành công

# import pandas library
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt

# create pandas DataFrame
df = pd.DataFrame[{'team': ['India', 'South Africa', 'New Zealand', 'England'],
                   'points': [10, 8, 3, 5],
                   'runrate': [0.5, 1.4, 2, -0.6],
                   'wins': [5, 4, 2, 2]}]

print[df]

đầu ra

           team  points  runrate  wins
0         India      10      0.5     5
1  South Africa       8      1.4     4
2   New Zealand       3      2.0     2
3       England       5     -0.6     2

Lý do 2 – Khai báo tên module thành tên biến

Chúng tôi gặp lỗi này nếu bạn đã đặt tên bất kỳ biến nào là '

raceback [most recent call last]:
  File "c:\Personal\IJS\Code\main.py", line 7, in 
    df = pd.dataframe[{'team': ['India', 'South Africa', 'New Zealand', 'England'],
    raise AttributeError[f"module 'pandas' has no attribute '{name}'"]

AttributeError: module 'pandas' has no attribute 'dataframe'. Did you mean: 'DataFrame'?
0' hoặc '
raceback [most recent call last]:
  File "c:\Personal\IJS\Code\main.py", line 7, in 
    df = pd.dataframe[{'team': ['India', 'South Africa', 'New Zealand', 'England'],
    raise AttributeError[f"module 'pandas' has no attribute '{name}'"]

AttributeError: module 'pandas' has no attribute 'dataframe'. Did you mean: 'DataFrame'?
1' và cố gắng tạo DataFrame.  

# import pandas library
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt

# variable named as pd
pd = [2, 3]
# create pandas DataFrame
df = pd.DataFrame[{'team': ['India', 'South Africa', 'New Zealand', 'England'],
                   'points': [10, 8, 3, 5],
                   'runrate': [0.5, 1.4, 2, -0.6],
                   'wins': [5, 4, 2, 2]}]

print[df]

đầu ra

Traceback [most recent call last]:
  File "c:\Personal\IJS\Code\main.py", line 9, in 
    df = pd.DataFrame[{'team': ['India', 'South Africa', 'New Zealand', 'England'],
AttributeError: 'str' object has no attribute 'DataFrame'

Trong đoạn mã trên của chúng tôi, chúng tôi có một biến cục bộ có tên '

raceback [most recent call last]:
  File "c:\Personal\IJS\Code\main.py", line 7, in 
    df = pd.dataframe[{'team': ['India', 'South Africa', 'New Zealand', 'England'],
    raise AttributeError[f"module 'pandas' has no attribute '{name}'"]

AttributeError: module 'pandas' has no attribute 'dataframe'. Did you mean: 'DataFrame'?
0' và Python sẽ không thể giải quyết DataFrame vì nó là biến cục bộ chứ không phải mô-đun Pandas

Chúng ta có thể giải quyết lỗi này bằng cách đổi tên biến thành tên khác

# import pandas library
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt

# variable named as pd
data = [2, 3]
# create pandas DataFrame
df = pd.DataFrame[{'team': ['India', 'South Africa', 'New Zealand', 'England'],
                   'points': [10, 8, 3, 5],
                   'runrate': [0.5, 1.4, 2, -0.6],
                   'wins': [5, 4, 2, 2]}]

print[df]

đầu ra

           team  points  runrate  wins
0         India      10      0.5     5
1  South Africa       8      1.4     4
2   New Zealand       3      2.0     2
3       England       5     -0.6     2

Lý do 3 – Đặt tên tệp là pd. py hoặc gấu trúc. py

Nếu bạn đã đặt tên cho tập lệnh là

raceback [most recent call last]:
  File "c:\Personal\IJS\Code\main.py", line 7, in 
    df = pd.dataframe[{'team': ['India', 'South Africa', 'New Zealand', 'England'],
    raise AttributeError[f"module 'pandas' has no attribute '{name}'"]

AttributeError: module 'pandas' has no attribute 'dataframe'. Did you mean: 'DataFrame'?
3 hoặc
raceback [most recent call last]:
  File "c:\Personal\IJS\Code\main.py", line 7, in 
    df = pd.dataframe[{'team': ['India', 'South Africa', 'New Zealand', 'England'],
    raise AttributeError[f"module 'pandas' has no attribute '{name}'"]

AttributeError: module 'pandas' has no attribute 'dataframe'. Did you mean: 'DataFrame'?
4 thì bạn sẽ gặp lỗi mô-đun 'gấu trúc' không có thuộc tính 'khung dữ liệu'.  

Điều này chủ yếu xảy ra vì tên tệp sẽ che khuất mô-đun Pandas và nó có thể làm rối quá trình nhập mô-đun

Chúng tôi có thể khắc phục sự cố này bằng cách đổi tên tập lệnh thành một số tên khác, chẳng hạn như “my_script. py”

Lý do 4- Gói Pandas chưa được cài đặt

Lý do cuối cùng có thể là nếu thư viện Pandas không được cài đặt trong đường dẫn Python thích hợp. Bạn có thể đi và xác minh đường dẫn

raceback [most recent call last]:
  File "c:\Personal\IJS\Code\main.py", line 7, in 
    df = pd.dataframe[{'team': ['India', 'South Africa', 'New Zealand', 'England'],
    raise AttributeError[f"module 'pandas' has no attribute '{name}'"]

AttributeError: module 'pandas' has no attribute 'dataframe'. Did you mean: 'DataFrame'?
5 và xem thư viện Pandas có tồn tại không.  

Chủ Đề