我是Python的新手(使用Anaconda 3.5)--之前有MATLAB的经验。非常感谢你的帮助。如果有更简单的方法,请让我知道。
我有一些从实验设备的pdf文件中读取和清理出来的数据,并将其附加到一个列表中:
>print(outputdata)
[[['2.37701'], ['-'], ['-'], ['-'], ['-'], ['18.95276'], ['5.07365e-1']], [['2.75613'], ['-'], ['-'], ['-'], ['-'], ['16.99642'], ['4.10023e-1']], [['1.80527'], ['-'], ['-'], ['-'], ['-'], ['20.75384'], ['4.58238e-1']], [['1.58721'], ['-'], ['-'], ['-'], ['-'], ['18.06942'], ['3.81128e-1']], [['1.98336'], ['-'], ['-'], ['-'], ['-'], ['18.20776'], ['3.64733e-1']], [['1.75710'], ['-'], ['-'], ['-'], ['-'], ['23.03760'], ['4.36234e-1']], [['1.58967'], ['-'], ['-'], ['-'], ['-'], ['21.43884'], ['3.88509e-1']], [['2.37701'], ['-'], ['-'], ['-'], ['-'], ['18.95276'], ['5.07365e-1']]]
我正在尝试将列表中每个元素的每个元素取出并保存到新列表中。我还想清理数据,删除括号和引号,只保留数字。我需要对其进行操作,因此计划将其转换为numpy数组,然后将其添加到DataFrame中,以便更轻松地导出到Excel(已经有导出代码了)。每个列向量对应于特定标题:
Molecule = ["H2", "Ar", "Methane", "Ethane", "Ethylene", "Propane(C3H8)", "Propylene"]
这是一个期望的 H2 数据示例:
2.37701
2.75613
1.80527
1.58721
1.98336
1.75710
1.58967
2.37701
我首先完成了以下操作:
outputdatalist = [x[0] for x in outputdata]
以下是输出结果:
[['2.37701'], ['2.75613'], ['1.80527'], ['1.58721'], ['1.98336'], ['1.75710'], ['1.58967'], ['2.37701']]
然后
for row in outputdatalist:
print(' '.join(row)) # I need to append this on every iteration
我的不太正统(且不成功)的方法是按照以下方式进行双重(三重?)循环:
outputdatalist = []
for counter, elem in enumerate(Molecule):
for counter1, elem1 in enumerate(outputdata):
outputdatalist[counter] = [x[counter1] for x in outputdata]
然后将每个outputdatalist[i]转换为np数组,然后使用类似以下内容的pd.DataFrame循环遍历:
pd.DataFrame({Molecule[i]: outputdatalist[i]})
[x[0][0] for x in output_data]
获取你想要的 "H2" 数据呢?在说完 "我的非 Pythonic(并且不成功)..." 之后,你要做什么?分子
突然出现了。 - blacksitepage_remove_spaces = [i.replace('-', '0') if i == '-' else i for i in page_remove_spaces]
然后将字符串转换为浮点数:df = df.astype(float)
在导出到Excel之前进行操作。 - Monty