【发布时间】:2025-12-12 06:15:01
【问题描述】:
我正在尝试将 SPSS 文件转换为 Pandas,它工作正常。但是,所有变量都显示为“b'variable_name'”。它在每个变量前面放置一个“b”,并在原始变量名称周围加上单引号。有没有办法做到这一点并保留原始变量名?
我尝试重命名变量,但引号会影响代码......此外......还有很多变量,所以这很乏味且不理想。
df = pd.DataFrame(list(s.SavReader(r'C:\Users\Nick\Desktop\GitProjects\Data\M2.sav', returnHeader=True,
recodeSysmisTo='NaN',ioUtf8=True,rawMode=True)))
df.head(10)
# Create a new variable called 'header' from the first row of the dataset
header = df.iloc[0]
# Replace the dataframe with a new one which does not contain the first row
df = df[1:]
# Rename the dataframe's column values with the header variable
M2 = df.rename(columns = header)
M2.head(10)
这是生成的日期范围。没关系,但我需要去掉每个变量周围的 'b' 和单引号。
【问题讨论】:
标签: python pandas dataframe spss