我在列入数据帧时遇到了麻烦。
dataInList =
<controls:DropShadowPanel Margin="40"
VerticalAlignment="Center"
HorizontalAlignment="Stretch"
HorizontalContentAlignment="Stretch"
>
<Grid Background="Red" Height="200" HorizontalAlignment="Stretch"/>
</controls:DropShadowPanel>
df = pd.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']
我该怎么办?
事先感谢!
答案 0 :(得分:2)
对list
列表的第一个值使用split
理解,然后添加strip
以删除尾随空格:
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)
编辑:
问题是有一些列表没有长度2,所以需要过滤它:
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)