【发布时间】:2017-09-25 15:49:43
【问题描述】:
有没有办法将数据从 SPSS 导出到 CSV,包括小数点前的零? 目前,我有“.41”,我想将“0.41”导出到我的 CSV 文件中。
有什么建议吗?
【问题讨论】:
标签: python pandas spss decimal-point
有没有办法将数据从 SPSS 导出到 CSV,包括小数点前的零? 目前,我有“.41”,我想将“0.41”导出到我的 CSV 文件中。
有什么建议吗?
【问题讨论】:
标签: python pandas spss decimal-point
直接在 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)
【讨论】: