# Pandas.read\_csv มี options อะไรน่าสนใจบ้าง

**URL:** <https://discuss.dataengineercafe.io/t/pandas-read-csv-options/117>\
**Category:** พูดคุยเกี่ยวกับ Data Tools ต่าง ๆ\
**Tags:** python, pandas\
**Created:** [February 17, 2022, 4:42pm UTC](https://discuss.dataengineercafe.io/t/pandas-read-csv-options/117 "2022-02-17T16:42:05Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![atb](https://yyz1.discourse-cdn.com/flex035/user_avatar/discuss.dataengineercafe.io/atb/32/8_2.png) [@atb](https://discuss.dataengineercafe.io/u/atb)\
**Post date:** [February 17, 2022, 4:42pm UTC](https://discuss.dataengineercafe.io/t/pandas-read-csv-options/117/1 "2022-02-17T16:42:06Z")

</div>

หากใช้ [pandas](https://pandas.pydata.org/) library อยู่แล้ว จะพบว่า [read\_csv](https://pandas.pydata.org/docs/reference/api/pandas.read_csv.html) เป็นฟังก์ชันที่ถูกใช้งานบ่อยมาก ใช้สำหรับอ่าน data จากไฟล์ .csv ให้อยู่ในรูปแบบของ DataFrame

บ่อยครั้งเราจะใช้แค่ `pandas.read_csv(filepath)` ก็เพียงพอแล้ว

> แต่ถ้าเราอยากแปลง data ให้พร้อมใช้มากขึ้นตั้งแต่อ่านไฟล์ล่ะ?

มาดูกันครับว่า `read_csv` มี options อะไรน่าสนใจบ้าง 🤩

สมมติว่าเรามี data หน้าตาประมาณนี้

```bash
$ import io
$ import pandas as pd
$ data="""int,float,date,str,space
001,3.31,2015/01/01,005, left space"""
$ df = pd.read_csv(io.StringIO(data))
$ df.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 1 entries, 0 to 0
Data columns (total 5 columns):
 # Column Non-Null Count Dtype
--- ------ -------------- -----
 0 int 1 non-null int64
 1 float 1 non-null float64
 2 date 1 non-null object
 3 str 1 non-null int64
 4 space 1 non-null object
dtypes: float64(1), int64(2), object(2)

```

เรามาลองแปลง data ด้วย options ของ `read_csv` กันครับ

### 1. แปลง data type ให้ columns ทั้งหมด ด้วย dtype

```bash
$ pd.read_csv(io.StringIO(data), dtype=object).info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 1 entries, 0 to 0
Data columns (total 5 columns):
 # Column Non-Null Count Dtype
--- ------ -------------- -----
 0 int 1 non-null object
 1 float 1 non-null object
 2 date 1 non-null object
 3 str 1 non-null object
 4 space 1 non-null object
dtypes: object(5)

```

### 2. แปลง data type บาง columns ด้วย dtype

```bash
$ pd.read_csv(io.StringIO(data), dtype={'int':'object'}).info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 1 entries, 0 to 0
Data columns (total 5 columns):
 # Column Non-Null Count Dtype
--- ------ -------------- -----
 0 int 1 non-null object
 1 float 1 non-null float64
 2 date 1 non-null object
 3 str 1 non-null int64
 4 space 1 non-null object
dtypes: float64(1), int64(1), object(3)

```

### 3. แปลง data type ให้เป็น datetime ด้วย parse\_dates

```bash
$ pd.read_csv(io.StringIO(data), parse_dates=['date']).info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 1 entries, 0 to 0
Data columns (total 5 columns):
 # Column Non-Null Count Dtype
--- ------ -------------- -----
 0 int 1 non-null int64
 1 float 1 non-null float64
 2 date 1 non-null datetime64[ns]
 3 str 1 non-null int64
 4 space 1 non-null object
dtypes: datetime64[ns](1), float64(1), int64(2), object(1)

```

### 4. แปลง data type ให้เป็น datetime ด้วย converters

```bash
$ pd.read_csv(io.StringIO(data), converters={'date': pd.to_datetime}).info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 1 entries, 0 to 0
Data columns (total 5 columns):
 # Column Non-Null Count Dtype
--- ------ -------------- -----
 0 int 1 non-null int64
 1 float 1 non-null float64
 2 date 1 non-null datetime64[ns]
 3 str 1 non-null int64
 4 space 1 non-null object
dtypes: datetime64[ns](1), float64(1), int64(2), object(1)

```

### 5. ลบ white space ใน column ด้วย converters

```bash
$ pd.read_csv(io.StringIO(data))['space']
0 left space
Name: space, dtype: object
$ pd.read_csv(io.StringIO(data), converters={'space': str.strip})['space']
0 left space
Name: space, dtype: object

```

`read_csv` มี options ให้ใช้เยอะมากก พี่ ๆ เพื่อน ๆ ลองตัวไหนบ้างแล้ว มาแชร์กันได้นะครับ 😄

### References

- [pandas.read\_csv — pandas 2.3.3 documentation](https://pandas.pydata.org/docs/reference/api/pandas.read_csv.html#pandas.read_csv)
- [python - What's the difference between dtype and converters in pandas.read\_csv? - Stack Overflow](https://stackoverflow.com/questions/34139102/whats-the-difference-between-dtype-and-converters-in-pandas-read-csv)

---

<div class="post-metadata">

**Author:** ![zkan](https://yyz1.discourse-cdn.com/flex035/user_avatar/discuss.dataengineercafe.io/zkan/32/2_2.png) [@zkan](https://discuss.dataengineercafe.io/u/zkan)\
**Post date:** [February 17, 2022, 4:56pm UTC](https://discuss.dataengineercafe.io/t/pandas-read-csv-options/117/2 "2022-02-17T16:56:33Z")

</div>

`converters` นี่เพิ่งเคยเห็นเลย ดีงาม 🤩 ปกติโหลดมาแล้วค่อยเอามาแปลงทีหลังตลอด แหะๆ 😂
