pandas reading CSV data formatted with comma for thousands separator

前端 未结 3 447
迷失自我
迷失自我 2020-11-28 15:16

I am trying to create a dataframe in pandas using a CSV that is semicolon-delimited, and uses commas for the thousands separator on numeric data. Is there a way to read this

相关标签:
3条回答
  • 2020-11-28 16:06

    Take a look at the read_csv documentation there is a keyword argument 'thousands' that you can pass the ',' into. Likewise if you had European data containing a '.' for the separator you could do the same.

    0 讨论(0)
  • 2020-11-28 16:10

    Pass param thousands=',' to read_csv to read those values as thousands:

    In [27]:
    import pandas as pd
    import io
    
    t="""id;value
    0;123,123
    1;221,323,330
    2;32,001"""
    pd.read_csv(io.StringIO(t), thousands=r',', sep=';')
    
    Out[27]:
       id      value
    0   0     123123
    1   1  221323330
    2   2      32001
    
    0 讨论(0)
  • 2020-11-28 16:11

    The answer to this question should be short:

    df=pd.read_csv('filename.csv', thousands=',')
    
    0 讨论(0)
提交回复
热议问题