从 full database.xml文件中提取approved药物的id、name、type和<experimental-properties>/<calculated-properties>中的Molecular Weight及另一个数值property。 将以上信息读入一个DataFrame中,保存为property.csv文件。 统计不同type药物的Molecular Weight和另一个数值property的平均值(降序排列)。 对不同type药物的Molecular Weight,绘制并显示箱型图
时间: 2024-02-17 07:04:15 浏览: 27
好的,我明白了。以下是我给出的解决方案:
首先,我们需要安装并导入所需的库和模块,包括`xml.etree.ElementTree`、`pandas`和`matplotlib`:
```python
import xml.etree.ElementTree as ET
import pandas as pd
import matplotlib.pyplot as plt
```
然后,我们可以使用`ElementTree`模块读取XML文件,并提取所需的信息。具体来说,我们需要找到每个approved药物的id、name和type,以及其对应的Molecular Weight和另一个数值property。这些信息存储在XML文件的不同节点中,因此我们需要使用XPath表达式逐个查找这些节点。具体来说,我们可以使用以下代码:
```python
# 读取XML文件
tree = ET.parse('full database.xml')
root = tree.getroot()
# 初始化数据
data = {'id': [], 'name': [], 'type': [], 'Molecular Weight': [], 'property': []}
# 查找每个approved药物的信息
for drug in root.findall(".//drug[./groups/group/text()='approved']"):
# 查找id、name和type
drug_id = drug.find('./drugbank-id[@primary="true"]').text
name = drug.find('./name').text
type = drug.find('./classification/description').text
# 查找Molecular Weight和property
mol_weight = None
property = None
for prop in drug.findall('.//property'):
if prop.find('./kind').text == 'Molecular Weight':
mol_weight = float(prop.find('./value').text)
elif property is None:
property = float(prop.find('./value').text)
# 将结果添加到数据中
if mol_weight is not None and property is not None:
data['id'].append(drug_id)
data['name'].append(name)
data['type'].append(type)
data['Molecular Weight'].append(mol_weight)
data['property'].append(property)
```
接下来,我们可以使用`pandas`库将数据转换为DataFrame,并将其保存到CSV文件中:
```python
# 将数据转换为DataFrame
df = pd.DataFrame(data)
# 保存为CSV文件
df.to_csv('property.csv', index=False)
```
然后,我们可以对不同类型的药物进行分组,并计算它们的Molecular Weight和另一个数值property的平均值,并按Molecular Weight平均值的降序排列。
```python
# 按type分组,并计算平均值
grouped = df.groupby('type').mean()
# 按Molecular Weight平均值降序排列
grouped = grouped.sort_values('Molecular Weight', ascending=False)
# 打印结果
print(grouped)
```
最后,我们可以绘制不同类型药物的Molecular Weight的箱型图:
```python
# 绘制箱型图
plt.figure(figsize=(10, 6))
df.boxplot(column=['Molecular Weight'], by='type')
plt.ylabel('Molecular Weight')
plt.show()
```
完整的代码如下:
```python
import xml.etree.ElementTree as ET
import pandas as pd
import matplotlib.pyplot as plt
# 读取XML文件
tree = ET.parse('full database.xml')
root = tree.getroot()
# 初始化数据
data = {'id': [], 'name': [], 'type': [], 'Molecular Weight': [], 'property': []}
# 查找每个approved药物的信息
for drug in root.findall(".//drug[./groups/group/text()='approved']"):
# 查找id、name和type
drug_id = drug.find('./drugbank-id[@primary="true"]').text
name = drug.find('./name').text
type = drug.find('./classification/description').text
# 查找Molecular Weight和property
mol_weight = None
property = None
for prop in drug.findall('.//property'):
if prop.find('./kind').text == 'Molecular Weight':
mol_weight = float(prop.find('./value').text)
elif property is None:
property = float(prop.find('./value').text)
# 将结果添加到数据中
if mol_weight is not None and property is not None:
data['id'].append(drug_id)
data['name'].append(name)
data['type'].append(type)
data['Molecular Weight'].append(mol_weight)
data['property'].append(property)
# 将数据转换为DataFrame
df = pd.DataFrame(data)
# 保存为CSV文件
df.to_csv('property.csv', index=False)
# 按type分组,并计算平均值
grouped = df.groupby('type').mean()
# 按Molecular Weight平均值降序排列
grouped = grouped.sort_values('Molecular Weight', ascending=False)
# 打印结果
print(grouped)
# 绘制箱型图
plt.figure(figsize=(10, 6))
df.boxplot(column=['Molecular Weight'], by='type')
plt.ylabel('Molecular Weight')
plt.show()
```
希望这能解决你的问题!