---
title:【Pandas】Pandas的兩種資料類型
---
# 【Pandas】Pandas的兩種資料類型
[TOC]
:::warning
:notebook_with_decorative_cover: **學習目標**:Series and DataFrame。
:::
:::warning
:calendar: **學習日程**
學習耗費時間:2021.06.13
筆記製作時間:2021.06.13
:::
Import numpy as np
Import pandas as pd
## Series
Series is a one-dimensional labeled array capable of holding any data type (integers, strings, floating point numbers, Python objects, etc.). The axis labels are collectively referred to as the index.
s = pd.Series(data, index=index)
Series格式就是一個一維陣列的資料,在這裡我們宣告一個pandas的Series類型資料,非常簡單的使用.Series()並將數值放到一個變數中。
Index可以不一定是要從0開始也可以是其他的內容
```python=
s=pd.Series(np.random(5),index:["a","b","c","d","e"])
pd.Series(5,index=["a","b","c"])
```
前面所提到的最基本的一個Series格式,其實之前提到的dictionary也可以變成Series。
```python=
d={"b":1,"a":0,"c",2}
```
## DataFrame
DataFrame is a 2-dimensional labeled data structure with columns of potentially different types. You can think of it like a spreadsheet or SQL table, or a dict of Series objects. It is generally the most commonly used pandas object.
df=pd.Datarame()
```python=
d = {
"one": pd.Series([1.0, 2.0, 3.0], index=["a", "b", "c"]),
"two": pd.Series([1.0, 2.0, 3.0, 4.0], index=["a", "b", "c", "d"])
}
df = pd.DataFrame(d)
df
```
| |one| two |
| -------- | -------- | -------- |
| a | 1.0 | 1.0 |
| b | 2.0 | 2.0 |
| c | 3.0 | 3.0 |
| d | NaN | 4.0 |
**分析**
---
```python
df.index
```
Index(['a', 'b', 'c', 'd'], dtype='object')
```python=
df['one'] #抓整行出來
```
| |one|
| -------- | -------- | -------- |
| a | 1.0 | 1.0 |
| b | 2.0 | 2.0 |
| c | 3.0 | 3.0 |
| d | NaN | 4.0 |
```python=
df["three"] = df["one"] * df["two"] #新稱新的欄位
```
| |one| two |three|
| -------- | -------- | -------- | -------- |
| a | 1.0 | 1.0 | 1.0|
| b | 2.0 | 2.0 | 4.0 |
| c | 3.0 | 3.0 | 9.0|
| d | NaN | 4.0 | NaN |
```python=
df["flag"] = df["one"] > 2
```
| |one| two |three|flag|
| -------- | -------- | -------- | -------- |-------- |
| a | 1.0 | 1.0 | 1.0| False|
| b | 2.0 | 2.0 | 4.0 | False|
| c | 3.0 | 3.0 | 9.0| True
| d | NaN | 4.0 | NaN | False
```python=
del df["two"] #刪除欄位
```
| |one| three| flag|
| -------- | -------- | -------- |-------- |
| a | 1.0 | 1.0 |False|
| b | 2.0 | 4.0 |False|
| c | 3.0 | 9.0 |True
| d | NaN | NaN |False|
{"metaMigratedAt":"2023-06-16T02:27:06.239Z","metaMigratedFrom":"Content","title":"【Pandas】Pandas的兩種資料類型","breaks":true,"contributors":"[{\"id\":\"73e4bb37-a564-414c-af21-4a14ee6e18ae\",\"add\":3081,\"del\":659}]"}