[转载] python将int转为string_python – 在Pandas中将列名从int转换为string

参考链接: 如何在Python中将字符串string转换为整数int

我有一个混合列名的pandas数据帧:

 1,2,3,4,5,’班级’

 当我将此数据帧保存到h5file时,它表示由于混合类型,性能将受到影响.如何在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]: df

 Out[27]:

 1 2 3 4 5 Class

 0 0.773423 0.865091 0.614956 0.219458 0.837748 0.862177

 1 0.544805 0.535341 0.323215 0.929041 0.042705 0.759294

 2 0.215638 0.251063 0.648350 0.353999 0.986773 0.483313

 In [28]: df.columns.map(type)

 Out[28]:

 array([, , , ,

 , ], dtype=object)

 In [29]: df.to_hdf("out.h5", "d1")

 C:\Anaconda3\lib\site-packages\pandas\io\pytables.py:260: PerformanceWarning:

 your performance may suffer as PyTables will pickle object types that it cannot

 map directly to c-types [inferred_type->mixed-integer,key->axis0] [items->None]

 f(store)

 C:\Anaconda3\lib\site-packages\pandas\io\pytables.py:260: PerformanceWarning:

 your performance may suffer as PyTables will pickle object types that it cannot

 map 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([, , , ,

 , ], dtype=object)

 In [32]: df.to_hdf("out.h5", "d1")

 In [33]:

 标签:python,pandas

 来源: https://codeday.me/bug/20191004/1853505.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值