tus*_*yer 3 python dataframe pandas categorical-data
如问题所述,我有一个df_original很大的数据框,但看起来像:
ID Count Column 2 Column 3 Column 4
RowX 1 234. 255. yes. 452
RowY 1 123. 135. no. 342
RowW 1 234. 235. yes. 645
RowJ 1 123. 115. no. 342
RowA 1 234. 285. yes. 233
RowR 1 123. 165. no. 342
RowX 2 234. 255. yes. 234
RowY 2 123. 135. yes. 342
RowW 2 234. 235. yes. 233
RowJ 2 123. 115. yes. 342
RowA 2 234. 285. yes. 312
RowR 2 123. 165. no. 342
.
.
.
RowX 1233 234. 255. yes. 133
RowY 1233 123. 135. no. 342
RowW 1233 234. 235. no. 253
RowJ 1233 123. 115. yes. 342
RowA 1233 234. 285. yes. 645
RowR 1233 123. 165. no. 342
Run Code Online (Sandbox Code Playgroud)
我试图摆脱文本数据,并将其替换为预定义的数值等效项。例如,在这种情况下,我想分别用或替换Column3的yes或no值。有没有办法无需我手动输入并更改值?10
v
RowX yes
RowY no
RowW yes
RowJ no
RowA yes
RowR no
RowX yes
RowY yes
RowW yes
RowJ yes
RowA yes
RowR no
Name: Column 3, dtype: object
Run Code Online (Sandbox Code Playgroud)
pd.factorize1 - pd.factorize(v)[0]
array([1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 0])
Run Code Online (Sandbox Code Playgroud)
np.wherenp.where(v == 'yes', 1, 0)
array([1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 0])
Run Code Online (Sandbox Code Playgroud)
pd.Categorical/astype('category')pd.Categorical(v).codes
array([1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 0], dtype=int8)
Run Code Online (Sandbox Code Playgroud)
v.astype('category').cat.codes
RowX 1
RowY 0
RowW 1
RowJ 0
RowA 1
RowR 0
RowX 1
RowY 1
RowW 1
RowJ 1
RowA 1
RowR 0
dtype: int8
Run Code Online (Sandbox Code Playgroud)
pd.Series.replacev.replace({'yes' : 1, 'no' : 0})
RowX 1
RowY 0
RowW 1
RowJ 0
RowA 1
RowR 0
RowX 1
RowY 1
RowW 1
RowJ 1
RowA 1
RowR 0
Name: Column 3, dtype: int64
Run Code Online (Sandbox Code Playgroud)
上面的一个有趣的通用版本:
v.replace({r'^(?!yes).*$' : 0}, regex=True).astype(bool).astype(int)
RowX 1
RowY 0
RowW 1
RowJ 0
RowA 1
RowR 0
RowX 1
RowY 1
RowW 1
RowJ 1
RowA 1
RowR 0
Name: Column 3, dtype: int64
Run Code Online (Sandbox Code Playgroud)
一切都不"yes"是0。
| 归档时间: |
|
| 查看次数: |
1401 次 |
| 最近记录: |