简体   繁体   中英

python pandas - unable to convert scientific notation in a csv field to string?

I'm trying to use pandas to make sure that a csv is outputting certain fields (containing numbers) as strings, not numbers.

Here's the initial fields in the csv - they are formatted in scientific notation:

FIPS_BLOCK  FIPS_BLKGR  FIPS_TRACT
5.51E+14    5.51E+11    5.51E+10
5.51E+14    5.51E+11    5.51E+10
5.51E+14    5.51E+11    5.51E+10
5.51E+14    5.51E+11    5.51E+10

How I'm trying to convert to string:

import pandas as pd

# lst of column names which needs to be string
lst_str_cols = ['FIPS_BLOCK', 'FIPS_BLKGR', 'FIPS_TRACT','FIPS_PLACE']
# use dictionary comprehension to make dict of dtypes
dict_dtypes = {x : 'str'  for x in lst_str_cols}
# use dict on dtypes
df = pd.read_csv(output_files_dir + "//" + output_shp_name + ".csv", dtype=dict_dtypes)
df.to_csv(output_files_dir + "//" + output_shp_name + ".csv")

It's still formatting in scientific notation. I've also tried converting to numeric (int64), but it still is outputting in scientific notation, so I'm not sure what else to try. Thank you.

Thanks.

From the pandas documentation you can add the quoting option in the .to_csv method.

For your code

import csv
df.to_csv(output_files_dir + "//" + output_shp_name + ".csv", quoting=csv.QUOTE_NONNUMERIC)

Or if you want to quote everything (including numeric), you could use csv.QUOTE_ALL

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM