【发布时间】:2021-09-17 04:53:40
【问题描述】:
我觉得基本的数据框使用正在查找相当于“键”以返回“值”,但我已经搜索和尝试了很多天都没有成功。所以我认为我没有尝试正确的事情,希望能得到任何帮助。
我已经尝试过 .to_dict() 并且无法弄清楚如何将这些值塑造成我可以查找的东西。制作由 XML 制成的数据框的字典也似乎效率低下。所以我又开始尝试 .loc[]。
Python:
# -*- coding: utf-8 -*-
# Importing the required libraries
import pandas as pd
#Define thing lookup dataframe columns and rows
thing_cols = ["Thing Name", "Thing ID"]
thing_rows = []
# Append rows, create and index the dataframe
thing_rows.append({"Thing Name": "thing 1 name",
"Thing ID": "thing_1_id"})
thing_rows.append({"Thing Name": "thing 2 name",
"Thing ID": "thing_2_id"})
thing_df = pd.DataFrame(thing_rows, columns=thing_cols)
thing_df = thing_df.set_index(list(thing_df.keys())[0])
print(thing_df.loc["thing 1 name"])
输出:
Thing ID thing_1_id
Name: thing 1 name, dtype: object
期望的输出:
thing_1_id
虽然上面只关注这个问题,但下面是我正在尝试做的事情的一个更大的图景,以防你看到一种更简单或更好的方法来从 XML 中获取我的相关事物 ID。
最终期望的输出:
,Collection item,RELATED-THING-IDs
0,name of Item 1,"thing_1_id, thing_2_id"
Python:
# -*- coding: utf-8 -*-
# Importing the required libraries
import lxml.etree as Xet
import pandas as pd
#Define main collection columns and rows for dataframe
coll_cols = ["Collection item", "RELATED-THING-IDs"]
coll_rows = []
#Define thing lookup dataframe columns and rows
thing_cols = ["Thing Name", "Thing ID"]
thing_rows = []
# Parsing the XML file
xmlparse = Xet.parse('sample.xml')
root = xmlparse.getroot()
for row in root:
# Create thing lookup dataframe
if (row.findtext('type') == "THING"):
thing_id = row.findtext("THING-ID")
thing_name = row.findtext("name")
thing_rows.append({"Thing Name": thing_name,
"Thing ID": thing_id})
thing_df = pd.DataFrame(thing_rows, columns=thing_cols)
thing_df = thing_df.set_index(list(thing_df.keys())[0])
# Find only collection items
if row.findtext('type') != "COLLECTION-ITEM":
continue
# Define values for collection item dataframe
name = row.findtext("name", "Missing name")
relat_thing_items = thing_df.loc[[row.xpath(
"./RELATED-THING/result/row/name/text()")],["THING-ID"]]
if len(relat_thing_items) > 0:
relat_thing_id = ', '.join(relat_thing_items)
else:
relat_thing_id = ""
coll_rows.append({"Collection item": name,
"RELATED-THING-IDs": relat_thing_id
})
coll_df = pd.DataFrame(coll_rows, columns=coll_cols)
# Writing dataframe to csv
coll_df.to_csv('output.csv')
XML:
<?xml version="1.0" encoding="UTF-8"?>
<result size="4321">
<row>
<type>CONTEXT</type>
<name>collections</name>
</row>
<row>
<type>COLLECTION-ITEM</type>
<name>name of Item 1</name>
<ITEM-ID>item_000001</ITEM-ID>
<RELATED-THING>
<result size="2">
<row>
<type>THING</type>
<name>thing name 1</name>
<no>1</no>
</row>
<row>
<type>THING</type>
<name>thing name 2</name>
<no>1</no>
</row>
</result>
</RELATED-THING>
</row>
<row>
<type>THING</type>
<name>thing name 1</name>
<THING-ID>thing_000783</THING-ID>
</row>
<row>
<type>THING</type>
<name>thing name 2</name>
<THING-ID>thing_000803</THING-ID>
</row>
</result>
【问题讨论】:
-
我想我知道如何 Looking up values in dataframe ,但我不明白你的问题。你能用一个简单的例子和一些随机数据来澄清一下吗?
-
index = ["Thing Name"]不是定义索引的正确方法 -
您需要数据框吗?或者你只是想把你的 XML 变成字典?
-
我很欣赏这个问题,但我不知道解决方案是否需要一个数据框。我需要 将 XML 转换为 CSV,并且愿意接受将其转换为该格式的方法。这篇文章 (stackoverflow.com/questions/69098095/…) 有关于整个项目的更多信息。我认识到有迹象表明我可能正在走一条比必要的道路更复杂的道路,我对更简单的解决方案持开放态度。哪些信息有助于确定数据框是否是有效的解决方案?
-
如果您需要将 XML 转换为 CSV,只进行依赖于常量或当前行的简单计算,那么您不需要数据框,您应该能够解析 XML 并编写 CSV 行使用
csv模块按行排列。如果您需要根据整个数据中的值(例如百分位数)进行某种程度的聚合或过滤,那么导入 pandas 是有意义的。您链接到的帖子很好地描述了如何解析 XML,而 Karina 的回答很好地演示了如何从数据框中选择值。
标签: python pandas dataframe lookup