I have a big .txt with data in bad formats. I would like to delete some rows and convert the rest of the data to floating point numbers. I would like to delete lines with 'X'or 'XX', the rest I have to convert to float, a number like this 4;00.1should be converted to 4.001. The file is as follows:
0,1,10/09/2012,3:01,4;09.1,5,6,7,8,9,10,11
1,-0.581586,11/09/2012,-1:93,0;20.3,739705,,0.892921,5,,6,7
2,XX,10/09/2012,3:04,4;76.0,0.183095,-0.057214,-0.504856,NaN,0.183095,12
3,-0.256051,10/09/2012,9:65,1;54.9,483293,0.504967,0.074442,-1.716287,7,0.504967,0.504967
4,-0.728092,11/09/2012,0:78,1;53.4,232247,4.556,0.328062,1.382914,NaN,4.556,4
5,4,11/09/2012,NaN,NaN,6.0008,NaN,NaN,NaN,6.000800,6.000000,6.000800
6,X,11/09/2012,X,X,5,X,8,2,1,17.000000,33.000000
7,,11/09/2012,,,,,,6.000000,5.000000,2.000000,2.000000
8,4,11/09/2012,7:98,3;04.5,5,6,3,7.000000,3.000000,3.000000,2
9,6,11/09/2012,2:21,4;67.2,5,2,2,7,3,8.000000,4.000000
I read it in a DataFrame and select the rows
from pandas import *
from csv import *
fileName = '~/data.txt'
colName = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l']
df = DataFrame(read_csv(fileName, names=colName))
print df[df['b'].isin(['X','XX',None,'NaN'])].to_string()
Exiting the last last line gives me only:
>>> print df[df['b'].isin(['X','XX',None,'NaN'])].to_string()
b c d e f g h i j k l
a
2 XX 10/09/2012 3:04 4;76.0 0.183095 -0.057214 -0.504856 NaN 0.183095 12 NaN
6 X 11/09/2012 X X 5.000000 X 8.000000 2 1.000000 17 33
Does not select row 7, and I would like to go through all df not only one column (the source file is very large).
At the moment, I use for conversion as shown below, but first you need to delete unnecessary lines in order to apply it to all df.
convert1 = lambda x : x.replace('.', '')
convert2 = lambda x : float(x.replace(';', '.'))
newNumber = convert2(convert1(df['e'][0]))
df, df.pop(), , . , . .txt df [0,3,8,9] 'c' , 'd' , - float. , , , pandas (, ) ndarray - ? .