「pandas」データ型objectのサンプル
環境
pandas 1.4.2
PyCharm 2021.3.3
構文
変数名 = pd.Series([文字列, np.nan])
object型は特殊なデータ型で、Pythonオブジェクトへのポインターを格納します。
np.nanは欠損値を表示します。
map()メソッドで各要素に組み込み関数type()を適用して型を確認します。
pandasでは文字列を含むSeriesやDataFrameの列はobject型となります。
使用例
import pandas as pd
import numpy as np
pdobj = pd.Series([0, 'studyskill', np.nan])
print("結果1")
print(pdobj)
print("結果2")
print(pdobj.map(type))
import pandas as pd
import numpy as np
pdobj = pd.Series([0, 'studyskill', np.nan])
print("結果1")
print(pdobj)
print("結果2")
print(pdobj.map(type))
import pandas as pd import numpy as np pdobj = pd.Series([0, 'studyskill', np.nan]) print("結果1") print(pdobj) print("結果2") print(pdobj.map(type))
実行結果
結果1
0 0
1 studyskill
2 NaN
dtype: object
結果2
0 <class 'int'>
1 <class 'str'>
2 <class 'float'>
dtype: object
結果1
0 0
1 studyskill
2 NaN
dtype: object
結果2
0 <class 'int'>
1 <class 'str'>
2 <class 'float'>
dtype: object
結果1 0 0 1 studyskill 2 NaN dtype: object 結果2 0 <class 'int'> 1 <class 'str'> 2 <class 'float'> dtype: object