【发布时间】:2019-11-05 14:16:44
【问题描述】:
考虑一个数据框。我想将一组列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)
为什么apply (axis=0) 会返回一个 Series,即使它应该逐列作用?
【问题讨论】: