Hướng dẫn how does python handle empty dataframe? - python xử lý khung dữ liệu trống như thế nào?

Để xem liệu DataFrame có trống không, tôi lập luận rằng người ta nên kiểm tra độ dài của chỉ mục cột của DataFrame:length of a dataframe's columns index:

if len(df.columns) == 0: 1

Reason:

Theo API tham chiếu Pandas, có một sự khác biệt giữa:

  • một khung dữ liệu trống với 0 hàng và 0 cột
  • một khung dữ liệu trống với các hàng chứa
    In [1]: import pandas as pd
            df1 = pd.DataFrame()
            df1
    Out[1]: Empty DataFrame
            Columns: []
            Index: []
    
    In [2]: len(df1.index)  # or len(df1)
    Out[2]: 0
    
    In [3]: df1.empty
    Out[3]: True
    
    2 do đó ít nhất 1 cột

Có thể cho rằng, họ không giống nhau. Các câu trả lời khác không chính xác trong đó

In [1]: import pandas as pd
        df1 = pd.DataFrame()
        df1
Out[1]: Empty DataFrame
        Columns: []
        Index: []

In [2]: len(df1.index)  # or len(df1)
Out[2]: 0

In [3]: df1.empty
Out[3]: True
3,
In [1]: import pandas as pd
        df1 = pd.DataFrame()
        df1
Out[1]: Empty DataFrame
        Columns: []
        Index: []

In [2]: len(df1.index)  # or len(df1)
Out[2]: 0

In [3]: df1.empty
Out[3]: True
4 hoặc
In [1]: import pandas as pd
        df1 = pd.DataFrame()
        df1
Out[1]: Empty DataFrame
        Columns: []
        Index: []

In [2]: len(df1.index)  # or len(df1)
Out[2]: 0

In [3]: df1.empty
Out[3]: True
5 không phân biệt và chỉ số trả lại là 0 và trống là đúng trong cả hai trường hợp.index is 0 and empty is True in both cases.

Ví dụ

Ví dụ 1: Một khung dữ liệu trống với 0 hàng và 0 cột

In [1]: import pandas as pd
        df1 = pd.DataFrame()
        df1
Out[1]: Empty DataFrame
        Columns: []
        Index: []

In [2]: len(df1.index)  # or len(df1)
Out[2]: 0

In [3]: df1.empty
Out[3]: True

Ví dụ 2: Một khung dữ liệu được đổ thành 0 hàng nhưng vẫn giữ lại các cột

In [1]: import pandas as pd
        df1 = pd.DataFrame()
        df1
Out[1]: Empty DataFrame
        Columns: []
        Index: []

In [2]: len(df1.index)  # or len(df1)
Out[2]: 0

In [3]: df1.empty
Out[3]: True
6

In [4]: df2 = pd.DataFrame({'AA' : [1, 2, 3], 'BB' : [11, 22, 33]})
        df2
Out[4]:    AA  BB
        0   1  11
        1   2  22
        2   3  33

In [5]: df2 = df2[df2['AA'] == 5]
        df2
Out[5]: Empty DataFrame
        Columns: [AA, BB]
        Index: []

In [6]: len(df2.index)  # or len(df2)
Out[6]: 0

In [7]: df2.empty
Out[7]: True

Bây giờ, xây dựng trên các ví dụ trước, trong đó chỉ mục là 0 và trống là đúng. Khi đọc độ dài của chỉ mục cột cho DataFrame DF1 được tải đầu tiên, nó sẽ trả về 0 cột để chứng minh rằng nó thực sự trống.length of the columns index for the first loaded dataframe df1, it returns 0 columns to prove that it is indeed empty.

In [8]: len(df1.columns)
Out[8]: 0

In [9]: len(df2.columns)
Out[9]: 2

Quan trọng, trong khi df2 DataFrame thứ hai không chứa dữ liệu, nó không hoàn toàn trống vì nó trả về số lượng cột trống vẫn tồn tại., while the second dataframe df2 contains no data, it is not completely empty because it returns the amount of empty columns that persist.

Tại sao nó quan trọng

Hãy thêm một cột mới vào các khung dữ liệu này để hiểu ý nghĩa:

# As expected, the empty column displays 1 series
In [10]: df1['CC'] = [111, 222, 333]
         df1
Out[10]:    CC
         0 111
         1 222
         2 333
In [11]: len(df1.columns)
Out[11]: 1

# Note the persisting series with rows containing `NaN` values in df2
In [12]: df2['CC'] = [111, 222, 333]
         df2
Out[12]:    AA  BB   CC
         0 NaN NaN  111
         1 NaN NaN  222
         2 NaN NaN  333
In [13]: len(df2.columns)
Out[13]: 3

Rõ ràng là các cột ban đầu trong DF2 đã bề mặt lại. Do đó, thay vào đó, nên đọc độ dài của chỉ mục các cột với

In [1]: import pandas as pd
        df1 = pd.DataFrame()
        df1
Out[1]: Empty DataFrame
        Columns: []
        Index: []

In [2]: len(df1.index)  # or len(df1)
Out[2]: 0

In [3]: df1.empty
Out[3]: True
7 để xem liệu dữ liệu có trống không.length of the columns index with
In [1]: import pandas as pd
        df1 = pd.DataFrame()
        df1
Out[1]: Empty DataFrame
        Columns: []
        Index: []

In [2]: len(df1.index)  # or len(df1)
Out[2]: 0

In [3]: df1.empty
Out[3]: True
7 to see if a dataframe is empty.

Giải pháp thực tế

# New dataframe df
In [1]: df = pd.DataFrame({'AA' : [1, 2, 3], 'BB' : [11, 22, 33]})
        df
Out[1]:    AA  BB
        0   1  11
        1   2  22
        2   3  33

# This data manipulation approach results in an empty df
# because of a subset of values that are not available (`NaN`)
In [2]: df = df[df['AA'] == 5]
        df
Out[2]: Empty DataFrame
        Columns: [AA, BB]
        Index: []

# NOTE: the df is empty, BUT the columns are persistent
In [3]: len(df.columns)
Out[3]: 2

# And accordingly, the other answers on this page
In [4]: len(df.index)  # or len(df)
Out[4]: 0

In [5]: df.empty
Out[5]: True
# SOLUTION: conditionally check for empty columns
In [6]: if len(df.columns) != 0:  # <--- here
            # Do something, e.g. 
            # drop any columns containing rows with `NaN`
            # to make the df really empty
            df = df.dropna(how='all', axis=1)
        df
Out[6]: Empty DataFrame
        Columns: []
        Index: []

# Testing shows it is indeed empty now
In [7]: len(df.columns)
Out[7]: 0

Việc thêm một chuỗi dữ liệu mới hoạt động như mong đợi mà không có sự bề mặt của các cột trống (thực tế, không có bất kỳ chuỗi nào chứa các hàng chỉ với

In [1]: import pandas as pd
        df1 = pd.DataFrame()
        df1
Out[1]: Empty DataFrame
        Columns: []
        Index: []

In [2]: len(df1.index)  # or len(df1)
Out[2]: 0

In [3]: df1.empty
Out[3]: True
2):

In [8]: df['CC'] = [111, 222, 333]
         df
Out[8]:    CC
         0 111
         1 222
         2 333
In [9]: len(df.columns)
Out[9]: 1

Để kiểm tra xem DataFrame có trống trong gấu trúc không, hãy sử dụng thuộc tính

In [1]: import pandas as pd
        df1 = pd.DataFrame()
        df1
Out[1]: Empty DataFrame
        Columns: []
        Index: []

In [2]: len(df1.index)  # or len(df1)
Out[2]: 0

In [3]: df1.empty
Out[3]: True
9.

In [1]: import pandas as pd
        df1 = pd.DataFrame()
        df1
Out[1]: Empty DataFrame
        Columns: []
        Index: []

In [2]: len(df1.index)  # or len(df1)
Out[2]: 0

In [3]: df1.empty
Out[3]: True
9 Trả về giá trị boolean cho biết liệu dữ liệu này có trống hay không. Nếu DataFrame trống,
In [4]: df2 = pd.DataFrame({'AA' : [1, 2, 3], 'BB' : [11, 22, 33]})
        df2
Out[4]:    AA  BB
        0   1  11
        1   2  22
        2   3  33

In [5]: df2 = df2[df2['AA'] == 5]
        df2
Out[5]: Empty DataFrame
        Columns: [AA, BB]
        Index: []

In [6]: len(df2.index)  # or len(df2)
Out[6]: 0

In [7]: df2.empty
Out[7]: True
1 sẽ được trả về. Nếu DataFrame không trống,
In [4]: df2 = pd.DataFrame({'AA' : [1, 2, 3], 'BB' : [11, 22, 33]})
        df2
Out[4]:    AA  BB
        0   1  11
        1   2  22
        2   3  33

In [5]: df2 = df2[df2['AA'] == 5]
        df2
Out[5]: Empty DataFrame
        Columns: [AA, BB]
        Index: []

In [6]: len(df2.index)  # or len(df2)
Out[6]: 0

In [7]: df2.empty
Out[7]: True
2 sẽ được trả về.

Ví dụ 1: DataFrame trống

Trong ví dụ này, chúng tôi sẽ khởi tạo một DataFrame trống và kiểm tra xem DataFrame có trống bằng thuộc tính

In [1]: import pandas as pd
        df1 = pd.DataFrame()
        df1
Out[1]: Empty DataFrame
        Columns: []
        Index: []

In [2]: len(df1.index)  # or len(df1)
Out[2]: 0

In [3]: df1.empty
Out[3]: True
9 không.

Chương trình Python

import pandas as pd

#initialize a dataframe
df = pd.DataFrame()

isempty = df.empty
print('Is the DataFrame empty :', isempty)

Chạy

In [4]: df2 = pd.DataFrame({'AA' : [1, 2, 3], 'BB' : [11, 22, 33]})
        df2
Out[4]:    AA  BB
        0   1  11
        1   2  22
        2   3  33

In [5]: df2 = df2[df2['AA'] == 5]
        df2
Out[5]: Empty DataFrame
        Columns: [AA, BB]
        Index: []

In [6]: len(df2.index)  # or len(df2)
Out[6]: 0

In [7]: df2.empty
Out[7]: True
4 Khởi tạo một khung dữ liệu trống. Và sau đó
In [1]: import pandas as pd
        df1 = pd.DataFrame()
        df1
Out[1]: Empty DataFrame
        Columns: []
        Index: []

In [2]: len(df1.index)  # or len(df1)
Out[2]: 0

In [3]: df1.empty
Out[3]: True
3 kiểm tra xem DataFrame có trống không. Vì DataFrame trống, chúng tôi sẽ nhận được giá trị boolean là
In [4]: df2 = pd.DataFrame({'AA' : [1, 2, 3], 'BB' : [11, 22, 33]})
        df2
Out[4]:    AA  BB
        0   1  11
        1   2  22
        2   3  33

In [5]: df2 = df2[df2['AA'] == 5]
        df2
Out[5]: Empty DataFrame
        Columns: [AA, BB]
        Index: []

In [6]: len(df2.index)  # or len(df2)
Out[6]: 0

In [7]: df2.empty
Out[7]: True
1 cho biến
In [4]: df2 = pd.DataFrame({'AA' : [1, 2, 3], 'BB' : [11, 22, 33]})
        df2
Out[4]:    AA  BB
        0   1  11
        1   2  22
        2   3  33

In [5]: df2 = df2[df2['AA'] == 5]
        df2
Out[5]: Empty DataFrame
        Columns: [AA, BB]
        Index: []

In [6]: len(df2.index)  # or len(df2)
Out[6]: 0

In [7]: df2.empty
Out[7]: True
7.

Đầu ra

Is the DataFrame empty : True

Ví dụ 2: DataFrame không trống

Trong ví dụ sau, chúng tôi đã khởi tạo một DataFrame với một số hàng và sau đó kiểm tra xem

In [1]: import pandas as pd
        df1 = pd.DataFrame()
        df1
Out[1]: Empty DataFrame
        Columns: []
        Index: []

In [2]: len(df1.index)  # or len(df1)
Out[2]: 0

In [3]: df1.empty
Out[3]: True
9 có trả lại
In [4]: df2 = pd.DataFrame({'AA' : [1, 2, 3], 'BB' : [11, 22, 33]})
        df2
Out[4]:    AA  BB
        0   1  11
        1   2  22
        2   3  33

In [5]: df2 = df2[df2['AA'] == 5]
        df2
Out[5]: Empty DataFrame
        Columns: [AA, BB]
        Index: []

In [6]: len(df2.index)  # or len(df2)
Out[6]: 0

In [7]: df2.empty
Out[7]: True
2 không.

Chương trình Python

In [1]: import pandas as pd
        df1 = pd.DataFrame()
        df1
Out[1]: Empty DataFrame
        Columns: []
        Index: []

In [2]: len(df1.index)  # or len(df1)
Out[2]: 0

In [3]: df1.empty
Out[3]: True
0

Chạy

Đầu ra

In [1]: import pandas as pd
        df1 = pd.DataFrame()
        df1
Out[1]: Empty DataFrame
        Columns: []
        Index: []

In [2]: len(df1.index)  # or len(df1)
Out[2]: 0

In [3]: df1.empty
Out[3]: True
1

Ví dụ 2: DataFrame không trống

Trong ví dụ sau, chúng tôi đã khởi tạo một DataFrame với một số hàng và sau đó kiểm tra xem

In [1]: import pandas as pd
        df1 = pd.DataFrame()
        df1
Out[1]: Empty DataFrame
        Columns: []
        Index: []

In [2]: len(df1.index)  # or len(df1)
Out[2]: 0

In [3]: df1.empty
Out[3]: True
9 có trả lại
In [4]: df2 = pd.DataFrame({'AA' : [1, 2, 3], 'BB' : [11, 22, 33]})
        df2
Out[4]:    AA  BB
        0   1  11
        1   2  22
        2   3  33

In [5]: df2 = df2[df2['AA'] == 5]
        df2
Out[5]: Empty DataFrame
        Columns: [AA, BB]
        Index: []

In [6]: len(df2.index)  # or len(df2)
Out[6]: 0

In [7]: df2.empty
Out[7]: True
2 không.

DataFrame trống trong Python là gì?

DataFrame.Empty.Đúng nếu ndframe hoàn toàn trống [không có vật phẩm], có nghĩa là bất kỳ trục nào có chiều dài 0. pandas.series.dropna, pandas.dataframe.dropna.Nếu ndframe chỉ chứa nans, nó vẫn không được coi là trống.True if NDFrame is entirely empty [no items], meaning any of the axes are of length 0. pandas.Series.dropna, pandas.DataFrame.dropna. If NDFrame contains only NaNs, it is still not considered empty.

Làm thế nào kiểm tra Pandas DF trống?

Để kiểm tra xem DataFrame có trống trong gấu trúc không, hãy sử dụng thuộc tính pandas.dataframe.eMpty.Thuộc tính này trả về giá trị boolean của true nếu khung dữ liệu này trống hoặc sai nếu khung dữ liệu này không trống.use pandas. DataFrame. empty attribute. This attribute returns a boolean value of true if this DataFrame is empty, or false if this DataFrame is not empty.

Pandas có nghĩa là bỏ qua nan?

Pandas trung bình () Các điểm chính theo mặc định bỏ qua các giá trị NAN và thực hiện trung bình trên trục chỉ mục.By default ignore NaN values and performs mean on index axis.