将SPSS数据导出为CSV时在小数点前添加零

问题描述:

有没有办法将数据从SPSS导出为包含小数点前的零的CSV? 目前,我有“.41”,我想将“0.41”导出到我的CSV文件中。将SPSS数据导出为CSV时在小数点前添加零

有什么建议吗?

似乎很难直接在SPSS中完成。 一个可能的答案:使用python + pandas。

import pandas as pd 

def add_leading_zero_to_csv(path_to_csv_file): 
    # open the file 
    df_csv = pd.read_csv(path_to_csv_file) 
    # you can possibly specify the format of a column if needed 
    df_csv['specific_column'] = df_csv['specific_column'].map(lambda x: '%.2f' % x) 
    # save the file (with a precision of 3 for all the floats) 
    df_csv.to_csv(path_to_csv_file, index=False, float_format='%.3g') 

有关“g”格式的更多信息:Format Specification Mini-Language

并注意浮点问题(例如,请参阅answer to this question