Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
488 views
in Technique[技术] by (71.8m points)

python - Change default decimal separator in pandas read_sql

I'm creating a python script, that create csv files as result from sybase queries.

When pandas creates the file, all numeric values inside csv file ends with ".0" (for example, 2.0 instead of 2 )

I can cast to integer in query, but it affects query performance.

In order to solve that, I setted coerce_float parameter to false inside read_sql function.

However all columns are converted to string, and some columns must be floats, and the decimal separator is "." and I need "," as decimal separator.

My question is: Is there some way to change default decimal separator as a comma and keep coerce_float to False?

Obs: a simple string replace doesnt solve my problem, because the script will read several query files.

question from:https://stackoverflow.com/questions/65910188/change-default-decimal-separator-in-pandas-read-sql

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

First of all, python uses "." to write floats so you cant have floats written with ",". If you want floats written with "," they must be strings. However, you can save floats with the decimal "," as you save the .csv, by passing the argument decimal=',' to df.to_csv().


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...