Change decimal separator (Python, Sqlite, Pandas)

眉间皱痕 提交于 2020-01-23 04:11:13

问题


I have an Excel spreadsheet (which is an extract from SAP) which has data in it (text, numbers). I turn this data into a DataFrame, do some calculations and finally save it to a sqlite database. Whereas the excel spreadsheet has a comma as the decimal separator. The sqlite database includes the numbers with a dot as the decimal separator.

Extract from the code:

df_mce3 = pd.read_excel('rawdata/filename.xlsx', header=0, converters={'supplier': str, 'month': str}, decimal=',')

(decimal=',' was a suggested solution which only works when you are working with csv)

After doing the calculations I use the following code to save the results to a sqlite database:

conn = sqlite.connect("database.db")
df_mce3.to_sql("rawdata", conn, if_exists="replace")
df_ka_ext.to_sql("costanalysis_external", conn, if_exists="replace")
[...]

Input:

month   ordqty  ordprice    ordervolume invoiceqty  invoiceprice    
08.2017 10,000  14,90       149,00      10,000      14,90

Output:

month   ordqty  ordprice    ordervolume invoiceqty  invoiceprice    
08.2017 10.000  14.90       149.00      10.000      14.90

I do need those numbers to have the same decimal separator as the input data and I cannot find a way to do this.

Therefore I am asking if someone of you has an idea how to achieve it?

I am using Python 3.5 with pandas (0.19.1) and numpy (1.11.2) on Mac OS X.

Thank you!


回答1:


After the comments I finally came up with a solution that works at least with sample data. I will have to check if it works with my actual data.

#!/usr/bin/env python3.5
import pandas as pd
import numpy as np

df = pd.DataFrame(np.random.randn(10, 4), columns=list('ABCD'))

def formatnumbers(x):
    x = round(x, 4)
    x = str(x).replace('.', ',')
    return x

for col in df.columns:
    df[col] =  df[col].apply(formatnumbers)

print(df.head(n=5))

Result is:

         A        B        C        D
0  -0,4065  -1,6113   0,2257   0,7424
1   0,8349   0,0316   -1,105  -1,6463
2  -0,2108   0,7356  -1,0823   0,5261
3   0,3382   0,6158   0,6117  -0,3896
4   -0,403  -1,3639   0,8691   0,5791



回答2:


You need to convert the float values before saving. Just loop through the column with values containing ., convert each value to string and then you can use replace method.

Here I converted all values in column x

df['x'] = [str(val).replace('.', ',') for val in df['x']]
df.to_sql('rawdata', conn, if_exists='replace')


来源:https://stackoverflow.com/questions/46263994/change-decimal-separator-python-sqlite-pandas

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!