如何在 pandas DataFrame 中替换多个分类中的值
How to replace values in multiple categoricals in a pandas DataFrame
我想替换包含多个分类的数据框中的某些值。
df = pd.DataFrame({'s1': ['a', 'b', 'c'], 's2': ['a', 'c', 'd']}, dtype='category')
如果我在单个列上应用 .replace
,结果如预期:
>>> df.s1.replace('a', 1)
0 1
1 b
2 c
Name: s1, dtype: object
如果我对整个数据帧应用相同的操作,则会显示错误(简短版本):
>>> df.replace('a', 1)
ValueError: Cannot setitem on a Categorical with a new category, set the categories first
During handling of the above exception, another exception occurred:
ValueError: Wrong number of dimensions
如果数据框包含整数作为类别,则会发生以下情况:
df = pd.DataFrame({'s1': [1, 2, 3], 's2': [1, 3, 4]}, dtype='category')
>>> df.replace(1, 3)
s1 s2
0 3 3
1 2 3
2 3 4
但是,
>>> df.replace(1, 2)
ValueError: Wrong number of dimensions
我错过了什么?
没有挖掘,这对我来说似乎是错误的。
我的工作
pd.DataFrame.apply
和 pd.Series.replace
这样做的好处是您无需更改任何类型。
df = pd.DataFrame({'s1': [1, 2, 3], 's2': [1, 3, 4]}, dtype='category')
df.apply(pd.Series.replace, to_replace=1, value=2)
s1 s2
0 2 2
1 2 3
2 3 4
或
df = pd.DataFrame({'s1': ['a', 'b', 'c'], 's2': ['a', 'c', 'd']}, dtype='category')
df.apply(pd.Series.replace, to_replace='a', value=1)
s1 s2
0 1 1
1 b c
2 c d
@cᴏʟᴅsᴘᴇᴇᴅ的工作
df = pd.DataFrame({'s1': ['a', 'b', 'c'], 's2': ['a', 'c', 'd']}, dtype='category')
df.applymap(str).replace('a', 1)
s1 s2
0 1 1
1 b c
2 c d
这种行为的原因是每列的分类值集不同:
In [224]: df.s1.cat.categories
Out[224]: Index(['a', 'b', 'c'], dtype='object')
In [225]: df.s2.cat.categories
Out[225]: Index(['a', 'c', 'd'], dtype='object')
因此,如果您要替换为 两个 类别中的值,它将起作用:
In [226]: df.replace('d','a')
Out[226]:
s1 s2
0 a a
1 b c
2 c a
作为一种解决方案,您可能希望手动将列分类,使用:
pd.Categorical(..., categories=[...])
其中类别将具有所有列的所有可能值...
我想替换包含多个分类的数据框中的某些值。
df = pd.DataFrame({'s1': ['a', 'b', 'c'], 's2': ['a', 'c', 'd']}, dtype='category')
如果我在单个列上应用 .replace
,结果如预期:
>>> df.s1.replace('a', 1)
0 1
1 b
2 c
Name: s1, dtype: object
如果我对整个数据帧应用相同的操作,则会显示错误(简短版本):
>>> df.replace('a', 1)
ValueError: Cannot setitem on a Categorical with a new category, set the categories first
During handling of the above exception, another exception occurred:
ValueError: Wrong number of dimensions
如果数据框包含整数作为类别,则会发生以下情况:
df = pd.DataFrame({'s1': [1, 2, 3], 's2': [1, 3, 4]}, dtype='category')
>>> df.replace(1, 3)
s1 s2
0 3 3
1 2 3
2 3 4
但是,
>>> df.replace(1, 2)
ValueError: Wrong number of dimensions
我错过了什么?
没有挖掘,这对我来说似乎是错误的。
我的工作
pd.DataFrame.apply
和 pd.Series.replace
这样做的好处是您无需更改任何类型。
df = pd.DataFrame({'s1': [1, 2, 3], 's2': [1, 3, 4]}, dtype='category')
df.apply(pd.Series.replace, to_replace=1, value=2)
s1 s2
0 2 2
1 2 3
2 3 4
或
df = pd.DataFrame({'s1': ['a', 'b', 'c'], 's2': ['a', 'c', 'd']}, dtype='category')
df.apply(pd.Series.replace, to_replace='a', value=1)
s1 s2
0 1 1
1 b c
2 c d
@cᴏʟᴅsᴘᴇᴇᴅ的工作
df = pd.DataFrame({'s1': ['a', 'b', 'c'], 's2': ['a', 'c', 'd']}, dtype='category')
df.applymap(str).replace('a', 1)
s1 s2
0 1 1
1 b c
2 c d
这种行为的原因是每列的分类值集不同:
In [224]: df.s1.cat.categories
Out[224]: Index(['a', 'b', 'c'], dtype='object')
In [225]: df.s2.cat.categories
Out[225]: Index(['a', 'c', 'd'], dtype='object')
因此,如果您要替换为 两个 类别中的值,它将起作用:
In [226]: df.replace('d','a')
Out[226]:
s1 s2
0 a a
1 b c
2 c a
作为一种解决方案,您可能希望手动将列分类,使用:
pd.Categorical(..., categories=[...])
其中类别将具有所有列的所有可能值...