首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何在python中将列标题从int转换为str

如何在python中将列标题从int转换为str
EN

Stack Overflow用户
提问于 2018-06-27 06:07:09
回答 1查看 78关注 0票数 -1

我从spss导入了一个文件(sav文件),然而,我的列的标题显示为整数而不是字符串。有没有办法解决这个问题?下面是我使用的代码……如果有任何帮助,我将非常感谢!

代码语言:javascript
复制
   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有一个简单的解决方案。提前感谢!

EN

回答 1

Stack Overflow用户

发布于 2018-06-27 07:43:42

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51051981

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档