Reputation: 45
I'm having troubles making a list into a dataframe.
dataInList=
["'2018-05-15 15:35:57\t\n', 'A', 'xtre','retle ",' 105.0 (C)\n'],
["'2018-05-15 15:35:57\t\n', 'A', 'xtre','retla ",' 0 (s*C)\n'],
["'2018-05-15 15:35:57\t\n', 'A', 'xtre','retla",' 0 (s*C)\n'],
["'2018-05-15 15:35:57\t\n', 'A', 'xtre','retke",' 0 (s)\n'],
["'2018-05-15 15:35:57\t\n', 'A', 'xtre','retds",' 0 (s)\n'],
["'2018-05-15 15:35:57\t\n', 'A', 'xtre','rewr",' 0 (s)\n'],
["'2018-05-15 15:35:57\t\n', 'A', 'xtre','sdff",' 0 (s)\n']
df = pd.DataFrame(dataInList) only recognizes two of the sample points as columns producing this:
0 1
1 '2018-05-15 15:35:57\t\n', 'A', 'xtre','..............' 101.5 (C)\n
2 '2018-05-15 15:35:57\t\n', 'A', 'xtre','..............' 105.0 (C)\n
3 '2018-05-15 15:35:57\t\n', 'A', 'xtre','..............' 118.0 (C)\n
4 '2018-05-15 15:35:57\t\n', 'A', 'xtre','..............' 110.0 (C)\n
5 '2018-05-15 15:35:57\t\n', 'A', 'xtre','..............' 110.0 (C)\n
How should I proceed?
Thanks on beforehand!
Upvotes: 1
Views: 48
Reputation: 863611
Use list
comprehension with split
first values of lists and then add strip
for remove trailing whitespaces:
df = pd.DataFrame([[y.strip() for y in x[0].split(',') + [x[1]]] for x in dataInList])
print (df)
0 1 2 3 4
0 '2018-05-15 15:35:57\t\n' 'A' 'xtre' 'retle 105.0 (C)
1 '2018-05-15 15:35:57\t\n' 'A' 'xtre' 'retla 0 (s*C)
2 '2018-05-15 15:35:57\t\n' 'A' 'xtre' 'retla 0 (s*C)
EDIT:
Problem is there are some list with no lenght 2, so need filter it:
dataInList = [["'2018-05-15 15:35:57\t\n', 'A', 'xtre','retle ",' 105.0 (C)\n'],
["'2018-05-15 15:35:57\t\n', 'A', 'xtre','retla ",' 0 (s*C)\n'],
["'2018-05-15 15:35:57\t\n', 'A', 'xtre','retla",' 0 (s*C)\n'],
[ "aaa"]]
df = pd.DataFrame([[y.strip() for y in x[0].split(',') + [x[1]]] for x in dataInList if len(x) == 2])
print (df)
0 1 2 3 4
0 '2018-05-15 15:35:57\t\n' 'A' 'xtre' 'retle 105.0 (C)
1 '2018-05-15 15:35:57\t\n' 'A' 'xtre' 'retla 0 (s*C)
2 '2018-05-15 15:35:57\t\n' 'A' 'xtre' 'retla 0 (s*C)
Upvotes: 2