首页 > 解决方案 > how to convert column titles from int to str in python

问题描述

I imported a file from spss, (sav file), however, the titles of my columns appear as integers instead of strings. Is there a way to fix it? Below is the code I used....I would apreciate any help!

   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()

Wondering whether there is a way to actually convert the titles from numbers to strings. It has happened as if it were excel, but excel has an easy fix for that. Thanks in advance!

标签: python-3.x

解决方案


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


推荐阅读