计算Python中json列的特定字符数

2024-10-01 04:58:47 发布

您现在位置:Python中文网/ 问答频道 /正文

假设我有一个数据帧df,如下所示:

         id                                              geojson
0  1010020478  {"name": "entities", "type": "FeatureCollectio...
1  1010020478  {"name": "entities", "type": "FeatureCollectio...
2  1010020478  {"name": "entities", "type": "FeatureCollectio...
3  1010020478  {"name": "entities", "type": "FeatureCollectio...
4  1010020478  {"name": "entities", "type": "FeatureCollectio...
5  1010020478  {"name": "entities", "type": "FeatureCollectio...
6  1010020478  {"name": "entities", "type": "FeatureCollectio...
7  1010020478  {"name": "entities", "type": "FeatureCollectio...
8  1010020478  {"name": "entities", "type": "FeatureCollectio...
9  1010020478  {"name": "entities", "type": "FeatureCollectio...

我想计算每一行的"type": "e""type":"e"的数量

{
  "name": "entities","type": "FeatureCollection","features": [
    {
      "type": "Feature","geometry": {
        "type": "Polygon","coordinates": [
          [
            [
              17.11460135135052,9.38631
            ],[
              15.205521351350516,9.38631
            ],[
              15.205521351350516,16.22718
            ],[
              25.228191351350503,16.22718
            ],[
              25.228191351350503,23.38623
            ],[
              7.728291351350526,23.38623
            ],[
              7.728291351350526,0
            ],[
              17.11460135135052,0
            ],[
              17.11460135135052,9.38631
            ]
          ]
        ]
      },"properties": {
        "area": "104","name": "201","type": "e","Layer": "0","SubClasses": "AcDbEntity:AcDbBlockReference","EntityHandle": "120A"
      }
    },{
      "type": "Feature","geometry": {
        "type": "Polygon","coordinates": [
          [
            [
              33.1826913513505,23.38623
            ],[
              25.228191351350503,23.38623
            ],[
              25.228191351350503,16.22718
            ],[
              33.1826913513505,16.22718
            ],[
              33.1826913513505,23.38623
            ]
          ]
        ]
      },"properties": {
        "area": "125","name": "202","type": "e","Layer": "0","SubClasses": "AcDbEntity:AcDbBlockReference","EntityHandle": "1223"
      }
    },{
      "type": "Feature","geometry": {
        "type": "Polygon","coordinates": [
          [
            [
              25.228191351350503,16.22718
            ],[
              15.205521351350516,16.22718
            ],[
              15.205521351350516,9.38631
            ],[
              25.228191351350503,9.38631
            ],[
              25.228191351350503,16.22718
            ]
          ]
        ]
      },"properties": {
        "area": " ","name": " ","type": " ","Layer": "0","SubClasses": "AcDbEntity:AcDbBlockReference","EntityHandle": "1232"
      }
    },{
      "type": "Feature","geometry": {
        "type": "Polygon","coordinates": [
          [
            [
              33.1826913513505,16.22718
            ],[
              25.228191351350503,16.22718
            ],[
              25.228191351350503,9.38631
            ],[
              26.182731351350505,9.38631
            ],[
              33.1826913513505,9.38631
            ],[
              33.1826913513505,16.22718
            ]
          ]
        ]
      },"properties": {
        "area": "22","name": " ","type": "p","Layer": "0","SubClasses": "AcDbEntity:AcDbBlockReference","EntityHandle": "123D"
      }
    },{
      "type": "Feature","geometry": {
        "type": "Polygon","coordinates": [
          [
            [
              38.1144813513505,14.95446
            ],[
              33.18269135135051,14.95446
            ],[
              33.18269135135051,9.38631
            ],[
              38.1144813513505,9.38631
            ],[
              38.1144813513505,14.95446
            ]
          ]
        ]
      },"properties": {
        "area": "20","name": " ","type": "t","Layer": "0","SubClasses": "AcDbEntity:AcDbBlockReference","EntityHandle": "1267"
      }
    }
  ]
}

我试过:

from collections import Counter

z = [""type": "e"", ""type":"e""]
df.geojson.str(Counter(z))

但我得到SyntaxError: invalid syntax。 请帮帮我。谢谢。你知道吗


Tags: namelayertypeareapropertiesfeatureentitiesgeometry
1条回答
网友
1楼 · 发布于 2024-10-01 04:58:47

以下是我的解决方案:

import pandas as pd
import json

def o_count(json_str):
    data = json.loads(json_str.replace(",", ","))
    try:
        result = []
        features = data['features']
        for x in features:
            result.append(x['properties']['type'])
        return result.count('o')
    except:
        return 0


df = pd.read_excel("test.xlsx")
df["num"] = df.apply(lambda row: o_count(row['geojson']), axis=1)

相关问题 更多 >