简体   繁体   English

Pandas 到 sql 同时保持小数

[英]Pandas to sql while keeping decimal

So I'm having pandas fetch a table containing name, date, price among other things from a website.所以我让 pandas 从网站获取包含名称、日期、价格等内容的表格。 This is done without hiccups.这是在没有打嗝的情况下完成的。 Problem is, I'd like to parse this data into a sqlite database.问题是,我想将此数据解析为 sqlite 数据库。 Everything runs smoothly except for the price, which has it's decimals removed.一切都运行顺利,除了价格,它的小数点被删除。

Prices are written in this style on the webpage and in the dataframe:价格在网页上和 dataframe 中以这种风格书写:

  • 9,20 9,20
  • 149,935 149,935
  • 23,431 23,431

and gets converted into this:并转换为:

  • 920 920
  • 149935 149935
  • 23431 23431

the pandas.DataFrame.to_sql does not contain any references to anything in the ballpark of this, as far as I understand it at least.至少据我所知, pandas.DataFrame.to_sql不包含任何对此范围内的任何内容的引用。

Code if anyone is interested:如果有人感兴趣,请代码:

    def fetcher(self):
    r = requests.get("https://")
    df_list = pd.read_html(r.text) # this parses all the tables in webpages to a list
    df_list[0].pop("Status") #removes column status
    df_list[0].pop("Unnamed: 15") #removes column "unnamed: 15", this is a horrible solution... but it works
    df_list[0].to_sql('mine321', conn)
    print(r.text)

The problem seemed to stem from when the DataFrame was converted into a list.问题似乎源于将 DataFrame 转换为列表时。

Changing out换掉

df_list = pd.read_html(r.text) df_list = pd.read_html(r.text)

into the following进入以下

df_list = pd.read_html(r.text, decimal=',', thousands='.') df_list = pd.read_html(r.text,十进制=',',千='。')

Seems to have solved it.似乎已经解决了。 Though I will not vouch for this being a particularly good solution, might end up messing something else up.虽然我不会保证这是一个特别好的解决方案,但最终可能会搞砸其他事情。 Works for my problem though.虽然适用于我的问题。

Change the format of your price column to "float", looks like it is set up as "int".将价格列的格式更改为“float”,看起来它设置为“int”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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