Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 如何在python中将列标题从int转换为str_Python 3.x - Fatal编程技术网

Python 3.x 如何在python中将列标题从int转换为str

Python 3.x 如何在python中将列标题从int转换为str,python-3.x,Python 3.x,我从spss导入了一个文件(sav文件),但是,我的专栏标题 显示为整数而不是字符串。有办法解决吗?下面是我使用的代码…我将感谢任何帮助 import fnmatch import sys # import sys import os import pandas as pd #pandas importer import savReaderWriter as spss # to import file from SPSS import io #import

我从spss导入了一个文件(sav文件),但是,我的专栏标题 显示为整数而不是字符串。有办法解决吗?下面是我使用的代码…我将感谢任何帮助

   import fnmatch  
   import sys # import sys
   import os
   import pandas as pd #pandas importer
   import savReaderWriter as spss # to import file from SPSS
   import io #importing io
   import codecs #to resolve the UTF-8 unicode
   with spss.SavReader('file_name.sav') as reader: #Should I add "Np"
        records = reader.all()
    with codecs.open('file_name.sav', "r",encoding='utf-8', errors='strict') 
   as fdata: # Not sure if the problems resides on this line
       df = pd.DataFrame(records)
   df.head()
想知道是否有办法将标题从数字转换成字符串。这就像是excel,但excel有一个简单的解决方法。
提前谢谢

创建数据框架后,您可以使用
df.columns=df.columns.map(str)
将列标题更改为字符串。

感谢您的建议@Farhan,在我添加您的建议后,标题仍然显示为数字。有什么想法吗?它们可能仍然显示为数字,但它们已在后台转换为字符串。
df.columns.map(type)
的输出将显示值的类型。在执行上述行之后,它们都应该是