Search code examples
pythonregexcsvread-csv

can not separate csv file with thousands and comma


I need to read csv file with comma and also string and numbers but number contains comma in it like 1,260. Also csv file is seperated by comma so i can not read file in right way. How could i seperate them?

import pandas as pd
df_customer_list=pd.read_csv("Customer_list 09.01.2024.csv",sep=',')

the file contains below 3 rows

angel melo,angelmelo56@gmail.com,"1,260",Yes,0
michael alem,michaleloo@gmail.com,60,Yes,0
charles ekk,charles56@gmail.com,"2,220",Yes,0

Solution

  • I think the core issue is that your data does not appear to have a header so that makes display of the dataframe a little wonky.

    Taking your example data, I seem to be able to load it ok by just specifying the thousands separator and no headers.

    import io
    import pandas
    
    data = """
    angel melo,angelmelo56@gmail.com,"1,260",Yes,0
    michael alem,michaleloo@gmail.com,60,Yes,0
    charles ekk,charles56@gmail.com,"2,220",Yes,0
    """
    
    df = pandas.read_csv(io.StringIO(data), thousands=",", header=None)
    print(df)
    

    Should produce:

                  0                      1     2    3  4
    0    angel melo  angelmelo56@gmail.com  1260  Yes  0
    1  michael alem   michaleloo@gmail.com    60  Yes  0
    2   charles ekk    charles56@gmail.com  2220  Yes  0