python-3.x - 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!
解决方案
创建 DataFrame 后,您可以使用df.columns = df.columns.map(str)
将列标题更改为字符串。
推荐阅读
- emacs - 更改 dired `long-list` 格式
- visual-c++ - 努力解决这个问题结构定义的数组作为函数参数
- shell - AWK - 匹配多个模式并打印结果
- mongodb - MongoDB 对其伪随机游标使用什么算法?
- c# - 如何使用 Visual Studio 2017 创建参考程序集
- c++ - Xcode “架构 x86_64 的未定义符号”
- python - 我对 python Django 'Login' 有问题没有属性 'filter'
- android - 如何在android中实现毕加索图片加载库?
- javascript - JavaScript 按相同的值对相似的对象进行分组
- jquery - Django应用程序中的Jquery Datepicker问题