考虑一个数据帧。我想将一组列to_convert
转换为类别。
我当然可以做到以下几点:
for col in to_convert:
df[col] = df[col].astype('category')
但我很惊讶,下面没有返回数据帧:
df[to_convert].apply(lambda x: x.astype('category'), axis=0)
当然,这会导致以下情况不起作用:
df[to_convert] = df[to_convert].apply(lambda x: x.astype('category'), axis=0)
为什么^{axis=0
)返回一个序列,即使它应该逐个作用于列?
这只是在master中修复的,在0.17.0中也会修复,请参见问题here
注意since pandas 0.23.0您不再
apply
将多个列转换为分类数据类型。现在您只需执行df[to_convert].astype('category')
(其中to_convert
是问题中定义的一组列)。相关问题 更多 >
编程相关推荐