【问题标题】:Adding a zero before the decimal point when exporting SPSS data to CSV将 SPSS 数据导出为 CSV 时在小数点前添加零
【发布时间】:2017-09-25 15:49:43
【问题描述】:

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

有什么建议吗?

【问题讨论】:

    标签: python pandas spss decimal-point


    【解决方案1】:

    直接在 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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-10-25
      • 1970-01-01
      • 1970-01-01
      • 2022-11-10
      • 1970-01-01
      • 2016-05-16
      • 2019-08-17
      • 1970-01-01
      相关资源
      最近更新 更多