pandas data with double quote

前端 未结 3 1415
遇见更好的自我
遇见更好的自我 2020-12-12 02:39

I am trying to read a large dataset in .csv format which is update automatically using the pandas library. The problem is that in my data, the first row is a string without

相关标签:
3条回答
  • 2020-12-12 03:18

    Just load the data with pd.read_csv() and then use .replace('"','', regex=True)

    In one line it would be:

    df = pd.read_csv(filename, sep=',').replace('"','', regex=True)
    

    To set the columns names:

    df.columns = df.iloc[0]
    

    And drop row 0:

    df = df.drop(index=0).reset_index(drop=True)
    
    0 讨论(0)
  • 2020-12-12 03:35

    you can replace " after read_csv and save that file again using df_csv.to_csv('fname')

    df_csv.apply(lambda x:x.str.replace('"', ""))
    
    0 讨论(0)
  • 2020-12-12 03:37

    Consider your data in a file data.csv like

    $> more data.csv 
    A,"B","C","D"
    comp_a,"tree","house","door"
    comp_b,"truck","red","blue"
    

    Perhaps a newer pandas version would solve your problem from itself, e.g. at pd.__version__ = '0.23.1'

    In [1]: import pandas as pd
    
    In [2]: pd.read_csv('data.csv')
    Out[2]: 
            A      B      C     D
    0  comp_a   tree  house  door
    1  comp_b  truck    red  blue
    

    Otherwise apply a replace on the read-out

    pd.read_csv('data.csv').replace('"', '')
    
    0 讨论(0)
提交回复
热议问题