熊猫与正则表达式

时间:2020-06-01 14:22:05

标签: python-3.x pandas

我正在尝试进行分析以获取不使用硬编码参数的出现次数。

我正在使用熊猫,这是item-argument列中的一些字符串示例:

<or>
<item-argument val="1234"/>
<item-argument val="123443"/>
<item-argument val="546546"/>
</or>
<and>
<item-argument val="53949"/>
<or>
<item-argument val="54125"/>
<item-argument val="4182"/>
</or>
</and>
<itemrule active="true" last-updated="2012-02-23T12:00:00" match="ANY" val="97063">
<item-argument current-event-only="true" val="38349"/>

我需要计算标签中的所有内容。

如果我进行硬编码,那将是

current_event_only = df['item-argument'].str.contains('current-event-only').value_counts()[True]
and_ = df['item-argument'].str.contains('<and>').value_counts()[True]
or_ = df['item-argument'].str.contains('<or>').value_counts()[True]
itemrule = df['item-argument'].str.contains('itemrule active').value_counts()[True]

,依此类推。如您所见,这将需要大量的硬编码,所以我想知道是否可以使用正则表达式以及如何使用它?

期望的输出将是每个的计数。与您使用COUNTIFS在Excel中实现的功能类似

current_event_only, 1
and_, 1
or_, 2
itemrule, 1

0 个答案:

没有答案