[英]Printing an XML file from DataFrame python with condition
I have a pandas df
like this:我有一个这样的熊猫
df
:
ID EpisodeID Origin Destination
1 1 A B
1 2 B A
2 1 C D
2 2 D E
2 3 E C
3 1 A D
3 2 D A
I want to make a txt file with this df
as source.我想用这个
df
作为源制作一个txt文件。 Therefore, I'm using a code such as this:因此,我正在使用这样的代码:
with open("output.txt","w+") as f:
for index, row in df.iterrows():
f.write(" <person id =\"%s\">\n" % (row['ID']))
f.write(" <activity O=\"%s"\ D=\"%s"\>\n % (row[Origin], row[Destination]))
f.write(" </activity>\n")
f.write(" </person>\n")
the output shows something like:输出显示如下:
<person id="1">
<activity O="A" D="B">
</activity>
</person>
<person id="1">
<activity O="B" D="A">
</activity>
</person>
However, what I'm trying to make is not like this.但是,我想做的不是这样。 How can I iterate or write the code so that the output will be something like:
我如何迭代或编写代码,以便输出类似于:
<person id="1">
<activity O="A" D="B">
</activity>
<activity O="B" D="A">
</activity>
</person>
<person id="2">
<activity O="C" D="D">
</activity>
<activity O="D" D="E">
</activity>
<activity O="E" D="C"
</activity>
</person>
So, what I'm trying to make for each ID, not for all index (if that makes any sense).所以,我试图为每个 ID 制作什么,而不是为所有索引制作(如果这有意义的话)。
Please help :)请帮忙 :)
Write a nested loop, group by ID
column first and write the person
tag for each group and within each group, loop and write the activity
:编写一个嵌套循环,首先按
ID
列分组,然后为每个组编写person
标签,在每个组内,循环并编写activity
:
with open("output.txt","w+") as f:
for _id, g in df.groupby('ID'):
f.write(f' <person id ="{_id}">\n')
for t in g.itertuples(): # use itertuples since it's faster than iterrows
f.write(f' <activity O="{t.Origin}" D="{t.Destination}">\n')
f.write(" </activity>\n")
f.write(" </person>\n")
Output:输出:
with open("output.txt", "r") as f:
print(''.join(f.readlines()))
<person id ="1">
<activity O="A" D="B">
</activity>
<activity O="B" D="A">
</activity>
</person>
<person id ="2">
<activity O="C" D="D">
</activity>
<activity O="D" D="E">
</activity>
<activity O="E" D="C">
</activity>
</person>
<person id ="3">
<activity O="A" D="D">
</activity>
<activity O="D" D="A">
</activity>
</person>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.