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
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)
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('"', ""))
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('"', '')