在 Pandas 中更改列的數據類型
阿新 • • 發佈:2018-06-03
value NPU HA span 使用 taf pos 日期 soft
2 對於
使用
另外
import pandas as pd
import numpy as np
a = [[‘a‘, ‘1.2‘, ‘4.2‘], [‘b‘, ‘70‘, ‘0.03‘], [‘x‘, ‘5‘, ‘0‘]]
df = pd.DataFrame(a)
df.dtypes
0 object
1 object
2 object
dtype: object
數據框(data.frame)是最常用的數據結構,用於存儲二維表(即關系表)的數據,每一列存儲的數據類型必須相同,不同數據列的數據類型可以相同,也可以不同,但是每列的行數(長度)必須相同。數據框的每列都有唯一的名字,在已創建的數據框上,用戶可以添加計算列。
1 創建 DataFrame 時指定類型
如果要創建一個 DataFrame
,可以直接通過 dtype
參數指定類型:
df = pd.DataFrame(data=np.arange(100).reshape((10,10)), dtype=np.int8)
df.dtypes
0 int8
1 int8
2 int8
3 int8
4 int8
5 int8
6 int8
7 int8
8 int8
9 int8
dtype: object
2 對於 Series
s = pd.Series([‘1‘, ‘2‘, ‘4.7‘, ‘pandas‘ , ‘10‘])
s
0 1
1 2
2 4.7
3 pandas
4 10
dtype: object
使用 to_numeric
轉為數值
默認情況下,它不能處理字母型的字符串‘pandas‘
pd.to_numeric(s) # or pd.to_numeric(s, errors=‘raise‘);
--------------------------------------------------------------------------- ValueError Traceback (most recent call last) pandas/_libs/src/inference.pyx in pandas._libs.lib.maybe_convert_numeric() ValueError: Unable to parse string "pandas" During handling of the above exception, another exception occurred: ValueError Traceback (most recent call last) <ipython-input-24-12f1203e2645> in <module>() ----> 1 pd.to_numeric(s) # or pd.to_numeric(s, errors=‘raise‘); C:\Program Files (x86)\Microsoft Visual Studio\Shared\Anaconda3_64\lib\site-packages\pandas\core\tools\numeric.py in to_numeric(arg, errors, downcast) 131 coerce_numeric = False if errors in (‘ignore‘, ‘raise‘) else True 132 values = lib.maybe_convert_numeric(values, set(), --> 133 coerce_numeric=coerce_numeric) 134 135 except Exception: pandas/_libs/src/inference.pyx in pandas._libs.lib.maybe_convert_numeric() ValueError: Unable to parse string "pandas" at position 3
可以將無效值強制轉換為NaN
,如下所示:
pd.to_numeric(s, errors=‘coerce‘)
0 1.0
1 2.0
2 4.7
3 NaN
4 10.0
dtype: float64
如果遇到無效值,第三個選項就是忽略該操作:
pd.to_numeric(s, errors=‘ignore‘)
0 1
1 2
2 4.7
3 pandas
4 10
dtype: object
3 對於多列或者整個 DataFrame
如果想要將這個操作應用到多個列,依次處理每一列是非常繁瑣的,所以可以使用 DataFrame.apply
處理每一列。
a = [[‘a‘, ‘1.2‘, ‘4.2‘], [‘b‘, ‘70‘, ‘0.03‘], [‘x‘, ‘5‘, ‘0‘]]
df = pd.DataFrame(a, columns=[‘col1‘,‘col2‘,‘col3‘])
df
col1 | col2 | col3 | |
---|---|---|---|
0 | a | 1.2 | 4.2 |
1 | b | 70 | 0.03 |
2 | x | 5 | 0 |
df[[‘col2‘,‘col3‘]] = df[[‘col2‘,‘col3‘]].apply(pd.to_numeric)
df.dtypes
col1 object
col2 float64
col3 float64
dtype: object
這裏「col2」和 「col3」根據需要具有 float64
類型
df.apply(pd.to_numeric, errors=‘ignore‘)
該函數將被應用於整個DataFrame
,可以轉換為數字類型的列將被轉換,而不能(例如,它們包含非數字字符串或日期)的列將被單獨保留。
另外 pd.to_datetime
和 pd.to_timedelta
可將數據轉換為日期和時間戳。
軟轉換——類型自動推斷
infer_objects()
方法,用於將具有對象數據類型的 DataFrame 的列轉換為更具體的類型。
df = pd.DataFrame({‘a‘: [7, 1, 5], ‘b‘: [‘3‘,‘2‘,‘1‘]}, dtype=‘object‘)
df.dtypes
a object
b object
dtype: object
然後使用 infer_objects()
,可以將列 ‘a‘
的類型更改為 int64
:
df = df.infer_objects()
df.dtypes
a int64
b object
dtype: object
astype
強制轉換
如果試圖強制將兩列轉換為整數類型,可以使用 df.astype(int)
。
a = [[‘a‘, ‘1.2‘, ‘4.2‘], [‘b‘, ‘70‘, ‘0.03‘], [‘x‘, ‘5‘, ‘0‘]]
df = pd.DataFrame(a, columns=[‘one‘, ‘two‘, ‘three‘])
df.dtypes
one object
two object
three object
dtype: object
df[[‘two‘, ‘three‘]] = df[[‘two‘, ‘three‘]].astype(float)
df.dtypes
one object
two float64
three float64
dtype: object
在 Pandas 中更改列的數據類型