栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

将列名从int转换为pandas中的字符串

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

将列名从int转换为pandas中的字符串

您可以简单地使用

df.columns = df.columns.astype(str)

In [26]: df = pd.Dataframe(np.random.random((3,6)), columns=[1,2,3,4,5,'Class'])In [27]: dfOut[27]:1         2         3         4         5     Class0  0.773423  0.865091  0.614956  0.219458  0.837748  0.8621771  0.544805  0.535341  0.323215  0.929041  0.042705  0.7592942  0.215638  0.251063  0.648350  0.353999  0.986773  0.483313In [28]: df.columns.map(type)Out[28]: array([<class 'int'>, <class 'int'>, <class 'int'>, <class 'int'>,       <class 'int'>, <class 'str'>], dtype=object)In [29]: df.to_hdf("out.h5", "d1")C:Anaconda3libsite-packagespandasiopytables.py:260: PerformanceWarning: your performance may suffer as PyTables will pickle object types that it cannotmap directly to c-types [inferred_type->mixed-integer,key->axis0] [items->None]  f(store)C:Anaconda3libsite-packagespandasiopytables.py:260: PerformanceWarning: your performance may suffer as PyTables will pickle object types that it cannotmap directly to c-types [inferred_type->mixed-integer,key->block0_items] [items->None]  f(store)In [30]: df.columns = df.columns.astype(str)In [31]: df.columns.map(type)Out[31]: array([<class 'str'>, <class 'str'>, <class 'str'>, <class 'str'>,       <class 'str'>, <class 'str'>], dtype=object)In [32]: df.to_hdf("out.h5", "d1")In [33]:


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/637188.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号