【问题标题】:Pandas dataframe convert_object with a certain columns to be type fixedPandas 数据框 convert_object 具有要固定类型的特定列
【发布时间】:2014-09-30 17:09:08
【问题描述】:
有人可以提供一个快速/最佳的解决方案来使用带有convert_numeric=True 的数据框convert_objects 方法,但不包括某些类型已知的列?
我的原始数据有 50 多列混合类型,我喜欢使用方便的convert_objects 方法将列转换为正确的类型。它的作用就像魅力一样,直到价值,例如。 'XBGH0102' 出现在 str 类型列中。然后将该列转换为 float64 类型并用 NaN 填充。
【问题讨论】:
标签:
python
pandas
dataframe
typeconverter
【解决方案1】:
你可以这样做:
columns = [ list_of_columns .... ]
frame_to_save = df[df.columns.difference(columns)]
frame_to_process = df[columns]
result = pd.concat([ frame_to_save,
frame_to_process.convert_objects(convert_numeric=True)],
axis=1).reindex(df.columns)