I have a CSV with two delimiters (;
) and (,
) it looks like this:
vin;vorgangid;eventkm;D_8_lamsoni_w_time;D_8_lamsoni_w_value
V345578;295234545;13;-1000.0,-980.0;7.9921875,11.984375
V346670;329781064;13;-960.0,-940.0;7.9921875,11.984375
I want to import it into a pandas data frame, with the (;
) acting as a column separator and (,
) as a separator for a list
or array
using float
as data type. So far I am using this method, but I am sure there is something easier out there.
aa=0;
csv_import=pd.read_csv(folder+FileName, ';')
for col in csv_import.columns:
aa=aa+1
if type(csv_import[col][0])== str and aa>3:
# string to list of strings
csv_import[col]=csv_import[col].apply(lambda x:x.split(','))
# make the list of stings into a list of floats
csv_import[col]=csv_import[col].apply(lambda x: [float(y) for y in x])
Asides from the other fine answers here, which are more pandas-specific, it should be noted that Python itself is pretty powerful when it comes to string processing. You can just place the result of replacing ';'
with ','
in a StringIO
object, and work normally from there:
In [8]: import pandas as pd
In [9]: from cStringIO import StringIO
In [10]: pd.read_csv(StringIO(''.join(l.replace(';', ',') for l in open('stuff.csv'))))
Out[10]:
vin vorgangid eventkm D_8_lamsoni_w_time \
V345578 295234545 13 -1000.0 -980.0 7.992188
V346670 329781064 13 -960.0 -940.0 7.992188
D_8_lamsoni_w_value
V345578 295234545 11.984375
V346670 329781064 11.984375