【问题标题】:Tallying number of times certain strings occur in Python计算某些字符串在 Python 中出现的次数
【发布时间】:2017-11-13 02:26:54
【问题描述】:

我正在研究影响不同国家/地区不同部门的事件数据库,并希望创建一个表格来记录每个国家/地区的事件率细分。

数据库看起来像这样的atm

Incident Name  |  Country Affected  |  Sector Affected
incident_1     |  US,TW,CN          |  Engineering,Media
incident_2     |  FR,RU,CN          |  Government
etc., etc.

我的目标是建立一个看起来像这样的:

Country  |  Engineering  |  Media  |  Government
CN       |  3            |  0      |  5
etc.

现在我的方法基本上是使用 if 循环来检查 country 列是否包含特定字符串(例如“CN”),如果返回 True 然后从集合中运行 Counter 以创建初始计数的字典,然后保存。

我的问题是如何将我们扩展到可以在整个数据库中运行的级别,以及如何实际保存 Counter 生成的字典。

【问题讨论】:

  • 您介意编辑您的帖子以显示您目前拥有的代码吗?
  • 您的数据库是否有可能被更改? (US,Engineering),(TW,Engineering),(CN,Engineering)等都应该是单独的行

标签: python database string list pandas


【解决方案1】:

pd.Series.str.get_dummiespd.DataFrame.dot

c = df['Country Affected'].str.get_dummies(sep=',')
s = df['Sector Affected'].str.get_dummies(sep=',')

c.T.dot(s)

    Engineering  Government  Media
CN            1           1      1
FR            0           1      0
RU            0           1      0
TW            1           0      1
US            1           0      1

更大的例子

np.random.seed([3,1415])

countries = ['CN', 'FR', 'RU', 'TW', 'US', 'UK', 'JP', 'AU', 'HK']
sectors = ['Engineering', 'Government', 'Media', 'Commodidty']

def pick_rnd(x):
    i = np.random.randint(1, len(x))
    j = np.random.choice(x, i, False)
    return ','.join(j)

df = pd.DataFrame({
        'Country Affected': [pick_rnd(countries) for _ in range(10)],
        'Sector Affected': [pick_rnd(sectors) for _ in range(10)]
    })

df

          Country Affected               Sector Affected
0                       CN              Government,Media
1  FR,TW,JP,US,UK,CN,RU,AU         Commodidty,Government
2                 HK,AU,JP                    Commodidty
3           RU,CN,FR,JP,UK  Media,Commodidty,Engineering
4  CN,RU,FR,JP,TW,HK,US,UK   Government,Media,Commodidty
5                    FR,CN                    Commodidty
6     FR,HK,JP,TW,US,AU,CN                    Commodidty
7  CN,HK,RU,TW,UK,US,FR,JP              Media,Commodidty
8                 JP,UK,AU             Engineering,Media
9                 RU,UK,FR                         Media

然后

c = df['Country Affected'].str.get_dummies(sep=',')
s = df['Sector Affected'].str.get_dummies(sep=',')

c.T.dot(s)

    Commodidty  Engineering  Government  Media
AU           3            1           1      1
CN           6            1           3      4
FR           6            1           2      4
HK           4            0           1      2
JP           6            2           2      4
RU           4            1           2      4
TW           4            0           2      2
UK           4            2           2      5
US           4            0           2      2

【讨论】:

  • 太棒了,我感激不尽。我以为我会经历大量的嵌入式循环,而它只有 2 行......
  • @Lublamai 很高兴我能帮上忙。
猜你喜欢
  • 2017-10-09
  • 2010-11-12
  • 2013-12-25
  • 2021-05-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多