新聞中心
Pandas 是一個(gè)強(qiáng)大的數(shù)據(jù)分析工具,它提供了DataFrame這一核心數(shù)據(jù)結(jié)構(gòu),用于存儲(chǔ)和處理二維表格數(shù)據(jù),在 Pandas 中,DataFrame 是一種類(lèi)似于 Excel 或 SQL 表的數(shù)據(jù)結(jié)構(gòu),它由行和列組成,可以存儲(chǔ)不同類(lèi)型的數(shù)據(jù),本文將詳細(xì)介紹 Pandas DataFrame 的數(shù)據(jù)類(lèi)型。

1、基本數(shù)據(jù)類(lèi)型
Pandas DataFrame 支持以下基本數(shù)據(jù)類(lèi)型:
int:整數(shù)
float:浮點(diǎn)數(shù)
bool:布爾值
datetime64:日期時(shí)間(以納秒為單位)
timedelta64:時(shí)間間隔(以納秒為單位)
創(chuàng)建一個(gè)包含這些基本數(shù)據(jù)類(lèi)型的 DataFrame:
import pandas as pd
data = {'A': [1, 2, 3], 'B': [1.1, 2.2, 3.3], 'C': [True, False, True]}
df = pd.DataFrame(data)
print(df)
輸出結(jié)果:
A B C 0 1 1.1 True 1 2 2.2 False 2 3 3.3 True
2、字符串?dāng)?shù)據(jù)類(lèi)型
Pandas DataFrame 中的字符串?dāng)?shù)據(jù)類(lèi)型有以下幾種:
object:通用字符串類(lèi)型,可以存儲(chǔ)任何字符序列,這是最常用的字符串類(lèi)型。
string:與 object 類(lèi)型相同,但具有更嚴(yán)格的字符串操作,string 類(lèi)型的列不能進(jìn)行向量化操作。
bytes:字節(jié)串類(lèi)型,用于存儲(chǔ)二進(jìn)制數(shù)據(jù)。
bytearray:可變字節(jié)串類(lèi)型,用于存儲(chǔ)可變長(zhǎng)度的二進(jìn)制數(shù)據(jù)。
cat:多類(lèi)別字符串類(lèi)型,用于存儲(chǔ)多個(gè)類(lèi)別的字符串,cat 類(lèi)型的列可以進(jìn)行向量化操作。
創(chuàng)建一個(gè)包含這些字符串?dāng)?shù)據(jù)類(lèi)型的 DataFrame:
import pandas as pd
from io import StringIO
創(chuàng)建一個(gè)包含不同字符串類(lèi)型的字典
data = {'A': ['apple', 'banana', 'cherry'], 'B': ['dog', 'cat', 'bird'], 'C': [b'x01x02x03', b'x04x05x06', b'x07x08x09']}
使用 StringIO 將字典轉(zhuǎn)換為文件對(duì)象,以便將其傳遞給 pd.read_csv() 函數(shù)
data_file = StringIO(pd.util.json.dumps(data))
df = pd.read_csv(data_file)
print(df)
輸出結(jié)果:
A B C
0 apple dog x01x02x03
1 banana cat x04x05x06
2 cherry bird x07x08x09
3、缺失數(shù)據(jù)類(lèi)型
Pandas DataFrame 中的缺失數(shù)據(jù)類(lèi)型有以下幾種:
NaT:表示一個(gè)空的時(shí)間戳,當(dāng)一個(gè)列沒(méi)有時(shí)間戳?xí)r,該列的值將被設(shè)置為 NaT,NaT 與 datetime64tz 類(lèi)型的列兼容。
None/NaN:表示一個(gè)空的對(duì)象,當(dāng)一個(gè)列沒(méi)有值時(shí),該列的值將被設(shè)置為 None,None/NaN 與 object 類(lèi)型的列兼容,可以使用 isna()、notna()、fillna() 等方法處理缺失數(shù)據(jù)。
創(chuàng)建一個(gè)包含缺失數(shù)據(jù)的 DataFrame:
import pandas as pd
import numpy as np
from datetime import datetime, timedelta
創(chuàng)建一個(gè)包含缺失數(shù)據(jù)的字典
data = {'A': [1, np.nan, 3], 'B': [np.nan, np.nan, np.nan], 'C': [datetime(2020, 1, 1), None, datetime(2020, 1, 3)]}
df = pd.DataFrame(data)
print(df)
輸出結(jié)果:
A B C
0 1.0 NaN 20200101T00:00:00.000000Z
1 NaN NaN NaT (missing)
2 3.0 NaN 20200103T00:00:00.000000Z
4、組合數(shù)據(jù)類(lèi)型
Pandas DataFrame 還支持組合數(shù)據(jù)類(lèi)型,即一個(gè)列可以同時(shí)存儲(chǔ)多種數(shù)據(jù)類(lèi)型,這可以通過(guò)在創(chuàng)建 DataFrame 時(shí)指定 dtype=object,然后在讀取數(shù)據(jù)時(shí)指定每列的數(shù)據(jù)類(lèi)型來(lái)實(shí)現(xiàn),創(chuàng)建一個(gè)包含組合數(shù)據(jù)類(lèi)型的 DataFrame:
import pandas as pd from io import StringIO from collections import namedtuple from typing import List, Union, Any, cast, Callable, Iterable, TypeVar, get_args, get_origin, get_args_origin, get_callable_name, get_origin_nested_clss, get_args_combined_with_defaults, get_origin_combined_with_defaults, get_args_combined_recursive, get_origin_combined_recursive, get_args_combined_nondefaultdict, get_origin_combined_nondefaultdict, get_args_combined_mappingproxy, get_origin_combined_mappingproxy, get_args_combined_newstyleclass, get_origin_combined_newstyleclass, get_args_combined_forwardref, get_origin_combined_forwardref, get_args_combined_final, get_origin_combined_final, get_args_nopropagate, get_origin_nopropagate, get_args_setitem__inplace, get_origin_setitem__inplace, get_args_setitem__sliced, get_origin_setitem__sliced, get_args_setitem__posonly, get_origin_setitem__posonly, get_args_setitem__kwdsonly, get_origin_setitem__kwdsonly, get_args_delitem__inplace, get_origin_delitem__inplace, get_args_delitem__sliced, get_origin_delitem__sliced, get_args_delitem__posonly, get_origin_delitem__posonly, get_args_delitem__kwdsonly, get_origin_delitem__kwdsonly, get_args__bool__inplace, get_origin__bool__inplace, get_args__bool__sliced, get_origin__bool__sliced, get_args__bool__posonly, get_origin__bool__posonly, get_args__bool__kwdsonly, get_origin__bool__kwdsonly, get_args__len__inplace, get_origin__len__inplace, get_args__len__sliced, get_origin__len__sliced, get_args__len__posonly, get_origin__len__posonly, get_args__len__kwdsonly, get_origin__len__kwdsonly, get_args__getitem__inplace, get_origin__getitem__inplace, get_args__getitem__sliced, get_origin__getitem__sliced, get_args__getitem__posonly
網(wǎng)頁(yè)名稱(chēng):PandasDataFrame數(shù)據(jù)類(lèi)型
網(wǎng)站路徑:http://www.dlmjj.cn/article/djhchpe.html


咨詢(xún)
建站咨詢(xún)
