# 判斷

## Empty

無資料狀態

> Empty DataFrame
>
> Columns: \[FIELD\_A, FIELD\_B, FIELD\_C, FIELD\_D]
>
> Index: \[]

{% hint style="warning" %}
無法用「is None」判斷(因為他仍舊有欄位名之類的東西存在不是None)
{% endhint %}

### DataFrame預設方法

```python
data.empty # True/False
```

### 長度==0

```python
len(data) == 0 # 不可以用size!理由與isNone相同
```

## Null

{% code title="判斷A欄位中值是否為None" %}

```python
data['A'].isnull()
```

{% endcode %}

#### 取出A欄位中值非None的資料列

```python
data[~data['A'].isnull()]
```

#### 取出AB欄位中值皆非None的資料列

```python
data[~data['A'].isnull() & ~data['B'].isnull()]
```

## NaN

顯示為NaN，代表「空值」，與「空對象Nonw」不同，是「numpy.float64」類型，判斷要用「pd.isna」。

```python
print(type(data['30'].loc[0]))
# <class 'numpy.float64'>

print(pd.isna(data['30'].loc[0]))
# True/False =>判斷「欄位30第一筆」資料是否為NaN
```

{% code title="過濾可以這樣寫" %}

```python
data = data[~pd.isna(data['30'])]
# 濾除「欄位30」為NaN的資料
```

{% endcode %}

> <https://www.cnblogs.com/zjuhaohaoxuexi/p/15865290.html>

## isin 是否含有

判斷是否包含传入的指定值，回傳是對應的整組boolean。

```python
data = data[data['PRODUCT'].isin(must_have_product)]
# data = data中PRODUCT欄位值有在must_have_product清單中的資料
# must_have_product是個list，但根據isin說明，應該也可以放入dist
```

參考：<https://www.gairuo.com/p/pandas-isin>


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://dinoin.gitbook.io/shi-py-bu-shi-pi/pandas/dataframe/pan-duan.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
