Python 将列名从int转换为pandas中的字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38577126/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Convert Column Name from int to string in pandas
提问by Dzung Nguyen
I have a pandas dataframe with mixed column names:
我有一个混合列名的熊猫数据框:
1,2,3,4,5, 'Class'
1,2,3,4,5, '类'
When I save this dataframe to h5file, it says that the performance will be affected due to mixed types. How do I convert the integer to string in pandas?
当我将此数据帧保存到 h5file 时,它说由于混合类型,性能会受到影响。如何在熊猫中将整数转换为字符串?
回答by DSM
You can simply use df.columns = df.columns.astype(str)
:
您可以简单地使用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([<class 'int'>, <class 'int'>, <class 'int'>, <class 'int'>,
<class 'int'>, <class 'str'>], 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([<class 'str'>, <class 'str'>, <class 'str'>, <class 'str'>,
<class 'str'>, <class 'str'>], dtype=object)
In [32]: df.to_hdf("out.h5", "d1")
In [33]:
回答by Aerin
You can simply use df.columns = df.columns.map(str)
你可以简单地使用 df.columns = df.columns.map(str)
DSM's first answer df.columns = df.columns.astype(str)
didn't work for my dataframe. (I got TypeError: Setting dtype to anything other than float64 or object is not supported)
DSM 的第一个答案df.columns = df.columns.astype(str)
对我的数据框不起作用。(我收到 TypeError:不支持将 dtype 设置为 float64 或 object 以外的任何内容)